Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: How to prevent private names from polluting derived types?

I was shocked today by the fact that this code snippet has an ambiguous name reference:

class A
{
private:
    typedef int Type;
};

class B
{
public:
    typedef int Type;
};

class D : A, B
{
    Type value;//error: reference to 'Type' is ambiguous
};

Hmm! Imagine you are the author of class A and your class has already been used everywhere by different people and different projects. One day you decide to rewrite your A class. Doesn't this mean that you cannot use any new (even private) name in your new class without breaking others' code?

What is the convention here?

like image 284
zwhconst Avatar asked May 07 '20 06:05

zwhconst


Video Answer


1 Answers

Yes you are right. Using the current format/logic/coding style might break others' code if you decide to make changes later. Try to use PIMPL or fully qualify symbols. You're example is a good one of why it's not a good idea to not use fully qualify symbols. It's the same issue with code like:

using namespace std;

The line of code above could break a lot of stuff.

like image 85
Gabriel Grigoras Avatar answered Oct 24 '22 20:10

Gabriel Grigoras