Pre and post increment operators in different classes - c++

I want to create traits that overload operators, to avoid code duplication. But when I try to put pre and post increment operators in different classes the compiler gives me an error: "operator ++ is ambiguous" Code:
class A
{
public:
A& operator++(){return *this;}
};
class B
{
public:
B operator++(int){return *this;}
};
class C:public A, public B
{
};
int main()
{
C c;
c++;
++c;
}
In my case it is possible to inherit B from A, but then the compiler can't find the post-increment operator. Why does this happen, and what is the correct way?

GCC rejects the code, because it first performs name lookup without considering the argument lists, and the name is found in two base classes. Clang accepts the code, but this is caused by a bug.
The solution is to add using and create a separate class which inherits from A and B, and then inherit C from that class.
class A
{
public:
A& operator++(){return *this;}
};
class B
{
public:
B operator++(int){return *this;}
};
class AAndB:public A, public B
{
public:
using A::operator++;
using B::operator++;
};
class C:public AAndB
{
};
int main()
{
C c;
c++;
++c;
}

Related

How to access the class member

Sorry for the bad title... I think the solution may exist on this site, but I cannot find it.
class A {
};
class B {
private:
int _b;
};
class C {
private:
A a; // a: I want to access _b in b
B b;
};
Let's say I have 3 classes like the code above, now the object a in class C wants to access the member _b in object b. Is there any method to achieve it?
I tried using friend, I wonder if I haven't used it in a proper way, because it made the code very complex, like this:
class B {
private:
int _b;
public:
B() : _b(5) {}
int get_b() {
return _b;
}
};
class A {
public:
int get_a(B& b) {
cout << b.get_b();
}
};
class C {
private:
friend class A;
A a;
B b;
public:
A& get_A() {
return a;
}
B& get_B() {
return b;
}
};
int main() {
C c;
c.get_A().get_a(c.get_B());
}
Thank you in advance.
EDIT
Sorry about the confusing code above, actually I want to implement a compiler using OO style. I think a compiler is made of lexer, parser, and symbol table and other things. So I think the relationship is:
class compiler {
private:
lexer l;
parser p;
symbol_table st;
...
};
And the parser and lexer need to access the symbol_table, that is why this question is put forward. I think this design is resemble to the real compiler "in my opinion", but it seems hard to implement... Any advice is appreciated.
In you example, A must be a friend of B and not of C to let it access members of B:
class A;
class B {
friend class A;
//...
};
// ....
In other terms, it should look like this:
#include<iostream>
using namespace std;
class A;
class B {
friend class A;
private:
int _b;
public:
B() : _b(5) {}
};
class A {
public:
int get_a(B& b) {
cout << b._b;
}
};
class C {
private:
A a;
B b;
public:
A& get_A() {
return a;
}
B& get_B() {
return b;
}
};
int main() {
C c;
c.get_A().get_a(c.get_B());
}
That being said, you should probably reconsider your design to avoid such a strong dependency between your classes and get rid of friends.
Your code looks really confused. I'm not sure what you are trying to do in the second piece of code. The only place you can enable access to members of class B is in class B. You can do this either through making those members public or protected, adding accessor functions for the members or setting another class to be a friend class of class B by adding a friend class declaration inside class B.
Personally I would go with accessor functions as this means you are reducing class to class dependencies. Any class that has internal access to class B, by being made a friend or having direct access to member variables because they are public, is then dependent on the structure of class B.

Classes & pure virtual

I have 4 classes.
class A, class B, class C, class D
Class C includes class A and class B and reference them:
The Header File:
class C
{
private:
A &a;
B &b;
int x;
int y;
int energy;
public:
C(A &a, B &b);
void print(void);
virtual void printAt(void);
CPP File includes:
void C::printAt(void)
{
// move cursor to the current x, y coordinates
b.gotoXY(x,y);
}
In class D, I make class C a friend class by (class D : public class C...)
Then I have a void printAt(void).
This all works, but how do I access the b class attributes (b.gotoXY..) from class D?
Hopefully this makes Sence.
Just put them in protected section:
class C {
protected:
A &a;
B &b;
...
};
NOTE: It has nothing to do with virtual methods.
The reason you cannot access them from D is because they are private, which means they are only accessible from within D itself. In order to be able to access them only from D or its subclasses, you need to use the protected access modifier instead:
class C
{
private:
int x;
int y;
int energy;
protected:
A &a;
B &b;
public:
C(A &a, B &b);
void print(void);
virtual void printAt(void);
/// ...
};
Now, a bit of terminology:
When you type class C : public D you are not making it a friend, you are inheriting from it. This means C will be a base class of D. A friend is another, related concept.
A friend of some class is another class which has access to its private properties. So, if you instead had made D a friend of C, you would have had access to a and b without having to make them protected. This would be accomplished as such:
class C
{
// Some code...
friend D;
//Lots of code ...
}
Please note that, for this to work, you need to declare D before C.
Now, which of these options should you use?
Ask yourself this question: is a D logically a more specific type of C? If so, it should use inheritance. If not, it may be better to make D have a member of type C and use the friend keyword. In either case, use friend sparingly, and only if there is necessarily a very tight relationship between the two classes (perhaps if D is a factory for type C and C has a private constructor.)
You should make intended members protected or make their classes friend to your class.
In addition, I feel you will have a problem when you instantiating an object from C because of uninitialized references.
class C
{
private:
A &a;
B &b;
// ...
public:
C(A &a, B &b) : a(a), b(b)
^^^^^^^^^^^^
// ...
};
when you want other class in inherit access to your attributes .dont private them
so you can choose protected or public.
for more detail you can go http://www.learncpp.com/cpp-tutorial/115-inheritance-and-access-specifiers/
for solve problem try below code
class C
{
protected://or public
A &a;
B &b;
int x;
int y;
int energy;
public:
C(A &a, B &b);
void print(void);
virtual void printAt(void);
and in class D
class D:public C
{
public:
void printAt(void);
};

how to prevent usage of class members not yet constructed?

I have the following classes:
class A
{
public:
A() { x = 0; std::cout<<"A default ctor()\n"; }
A(int x_) { x = x_; std::cout<<"A normal ctor()\n"; }
int x;
};
class B
{
public:
B() { std::cout<<"B ctor()\n"; }
private:
std::string str;
};
and a function which creates an object B, taking an object A as parameter:
B
createB(const A& a) {
std::cout<<"a int: "<<a.x<<"\n";
return B();
}
if I design a class C, which has members of type A and B and constructs the B-object before A-object is constructed but using the A object to do so, this will compile without warnings but it will silently enter a bug:
class C
{
public:
C(): b(createB(a)), a(10) {}
private:
B b;
A a;
};
int main()
{
C c;
return 0;
}
Of course, the above example is a trivial one, but I've seen it in real world, in much more complex code (it's Friday, 8:30 PM and I just fixed this bug which led to segfaults).
How can I prevent this from happening?
I would agree with what others have suggested, namely that the onus is on the designer to ensure that objects are initialized before use. I see two ways of doing that in your case:
First (and easiest), reverse the order of a and b in the class definition:
class C
{
public:
C(): b(createB(a)), a(10) {}
private:
A a;
B b;
};
Second, you could move a to a base class if you want to really emphasize that it's initialization occurs before that of other members:
class CBase
{
protected:
CBase(): a(10) {}
protected:
A a;
};
class C : private CBase
{
public:
C(): b(createB(a)) {}
private:
B b;
};
I see three possible alternatives:
Require A to be constructed prior to constructing C:
class C
{
public:
C(const A& a) : a_(a), b_(a) {}
private:
A a_;
B b_;
};
Construct A prior to constructing 'B':
Delaying the construction of B until A is complete. This stops the undefined behavior from happening, but doesn't enforce proper behavior via the interface (as options 1 and 3 do)
class C
{
public:
C() : a_(/* construct as appropriate */)
{
b_.reset(new B(a_));
}
private:
A a_;
std::unique_ptr<B> b_;
};
If the design allows for it, have B contain (and expose) A.
This appears possible from the trivial example, but in real life may not be:
class B
{
public:
const A& my_a() {return a_;}
private:
// construct as appropriate (?)
A a_;
};
class C
{
private:
B b_;
};

Typecast operator overloading problem

For example i have two classes A and B, such that for two objects a and b, i want to be able to do :
A a;
B b;
a = b;
b = a;
for this i have overloaded the = operator, and the typecast operators as:
class A{
-snip-
operator B()const { return B(pVarA); }
};
class B{
-snip-
operator A()const { return A(pVarB); }
};
but when i try to compile this code, gcc throws the error :
error: expected type-specifier before 'B'
for the line: operator B()const { return B(pVarA);}
my guess is, this is due to a chicken and egg problem as class B is defined after class A.
Is there a way to circumvent this while still using the overloaded typecast operators. And if not, then what might be the best way to achieve my goals.
Any help will be appreciated. Thanks in advance.
Try forward declaring then supplying the actual function definitions later on:
class B;
class A{
-snip-
operator B()const;
};
class B{
-snip-
operator A()const;
};
inline A::operator B() const
{
return B(pVarA);
}
inline B::operator A() const
{
return A(pVarB);
}
This should work:
class B;
class A{
operator B()const;
};
class B{
operator A()const { return A(pVarB); }
};
A::operator B() const { return B(pVarA); }

mutually referential classes yield "incomplete type" error

I have a situation in which A has a reference to a class C defined inside B, and C has an instance of class B.
When I try to compile the code below, I get "field a has incomplete type". I assume this is because the compiler does not know how much memory it should allocate for an instance of A.
class A;
class B {
public:
class C {
A a;
};
};
class A {
A(const B::C& _c)
: c(_c)
{}
const B::C& c;
};
But when I try to compile this, I get "C in class B does not name a type":
class B;
class B::C;
class A {
A(const B::C& _c)
: c(_c)
{}
const B::C& c;
};
class B {
public:
class C {
A a;
};
};
How can I convince the compiler that B::C is a real type?
As an absolute guess, I notice there's one permutation you haven't tried:
class B {
public:
class C; // Forward declaration
};
class A {
A(const B::C& _c)
: c(_c)
{}
const B::C& c;
};
class B::C {
A a;
C() : a(*this) {} // Thanks Nim for pointing this out!
};
This is quite possibly illegal, but worth a shot I think. If it doesn't work, then I don't see any way around the problem.
The forward declaration for A doesn't serve a purpose: you can't declare an instance of an incomplete type.
As to B::C, I don't think you can use nested names in an incomplete type. Just don't nest C in B: as far as I know this doesn't give you any significant advantages* and stops you from forward declaring it.
*The only advantage I can think of is that you can define it in the private section, but then A would have no business with it in the first place.