Hi,sir,please look at this code.

#include <iostream> 
using namespace std; 
 
class A{ 
private: 
    class B;
public: 
int out(int x){ 
return outB(x)->a; 
} 
B * outB(int x); 
 
private: 
B* root; 
 
class B{ 
int a; 
B(int init_a) :a(init_a){} 
friend class A; 
}; 
 
}; 
 
 
A::B* A::outB(int x){ 
root = new B(x); 
return root; 
} 
 
 
int main(int argc, char *argv[]) 
{ 
A a1; 
cout<<a1.out(8)<<endl; 
}

I want to use "typedef A::B C;" to make the code more readable.
But I don't know where should I put typedef?
Thank you very much!

Recommended Answers

All 3 Replies

How would that make things more readable? B is only accessible to A, so the only place you can create the typedef is inside A, and the only place you need to qualify it with A:: is in an out of line definition. If you typedef B as C, you still need to qualify it with A:: in an out of line definition. You'd actually be making the code less readable.

How would that make things more readable? B is only accessible to A, so the only place you can create the typedef is inside A, and the only place you need to qualify it with A:: is in an out of line definition. If you typedef B as C, you still need to qualify it with A:: in an out of line definition. You'd actually be making the code less readable.

Yes,thank you! I have understood.

I also have a new question. Does the member initialization list can initialize an array like this:

1.class BinaryNode
2. {
3.  int element;
4.   BinaryNode *link[2];
5.    
6.  public:
7.   explicit BinaryNode(const int & theElement,
8. BinaryNode *lt=0,BinaryNode *rt=0) 
9.   : element(theElement),link[0](lt),link[1](rt){ }
10. };

My compier note that an error in the 8th line: error:expected '('before'['token Is that the problem of the member intialization list ?
Thank you!

OK ,I have solved it.
Put the array initialization into the constructor {}, then everything is OK.
So,C++ don't allow to initialize the array data member in member initialization list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.