| | |
Simple C++ Class problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 2
Reputation:
Solved Threads: 0
Hey, guys I think this is a very simple problem and that I just can't get it is due to some gap in my knowledge. Any way, what is wrong with this :
By does not work, I mean compile.
Thanks up front.
C++ Syntax (Toggle Plain Text)
class A{ private: int value; public : A(int v); }; A::A(int v) {value = v;} //Does not work class B {A ob(5);}; //Works main(){A ob(5);}
By does not work, I mean compile.
Thanks up front.
Last edited by Photomotoz; Nov 24th, 2008 at 3:50 pm.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
You should write your B class like this:
and then in B constructor add
I think you can't call constructors from within a class declaration.
C++ Syntax (Toggle Plain Text)
class B { A *ob; };
ob = new A(5); I think you can't call constructors from within a class declaration.
Last edited by mrboolf; Nov 24th, 2008 at 4:25 pm.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
I don't understand what you are trying to do - but my suggestion was to write a constructor for your B class like this so that every time an object of type B is created, its member A object will be initialized with the chosen value. Of course you may pass parameters to the constructor of B and forward them to the constructor of A - just don't forget to free the memory by putting
Hope this helps.
C++ Syntax (Toggle Plain Text)
B::B() { ob = new A(5); }
delete A; in B's destructor.Hope this helps.
Last edited by mrboolf; Nov 24th, 2008 at 4:53 pm.
•
•
Join Date: Nov 2008
Posts: 392
Reputation:
Solved Threads: 72
I think a little care in needed here. Poster #4 suggested making a heap object of A. [new A(5)] but that is maybe not what you want. It adds a level of complexity that might not be needed. You have to delete ob in B's destructor and manage the memory carefully in the copy constructor.
So what about
That way you have a A object in each B, and it goes out of scope on with with the corresponding B object.
It you want to pass parameters to B from A, try this.
So what about
c++ Syntax (Toggle Plain Text)
class A { private: int value; public: A(int); // constructor taking a value }; class B { private: A obj; public: B(); // Default Constructor }; A::A(int v) : value(v) {} B::B() : Obj(5) {} int main() { A ob(8); B bItem; }
That way you have a A object in each B, and it goes out of scope on with with the corresponding B object.
It you want to pass parameters to B from A, try this.
c++ Syntax (Toggle Plain Text)
class B { private: A obj; A secondObj; public: B(int,int); // Constructor }; B::B(int a,int b) : Obj(a),SecondObj(b) int main() { B bItem(3,4); // Make Obj(3) and secondObj(4) }
![]() |
Similar Threads
- Class Date (C++)
- Some Help with a simple C++ class. (C++)
- Simple array/class problem (dot operator)??? (C++)
- DrawHouse Code Problem (Java)
Other Threads in the C++ Forum
- Previous Thread: what's the difference between using an ordinary pointer and a "pointer to member"?
- Next Thread: Key input
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





