Simple C++ Class problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 2
Reputation: Photomotoz is an unknown quantity at this point 
Solved Threads: 0
Photomotoz Photomotoz is offline Offline
Newbie Poster

Simple C++ Class problem

 
0
  #1
Nov 24th, 2008
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 :
  1. class A{
  2. private: int value;
  3. public : A(int v);
  4. };
  5.  
  6. A::A(int v)
  7. {value = v;}
  8.  
  9. //Does not work
  10. class B {A ob(5);};
  11. //Works
  12. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Simple C++ Class problem

 
0
  #2
Nov 24th, 2008
You should write your B class like this:
  1. class B {
  2. A *ob;
  3. };
and then in B constructor add 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: Photomotoz is an unknown quantity at this point 
Solved Threads: 0
Photomotoz Photomotoz is offline Offline
Newbie Poster

Re: Simple C++ Class problem

 
0
  #3
Nov 24th, 2008
Hey, thanks for the reply.

What do you mean by
"in B constructor add ob = new A(5);"

What I am trying to do is make several objects of a class with several parameters within another class, which then in return gets called in main.
Last edited by Photomotoz; Nov 24th, 2008 at 4:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 182
Reputation: mrboolf will become famous soon enough mrboolf will become famous soon enough 
Solved Threads: 18
mrboolf mrboolf is offline Offline
Junior Poster

Re: Simple C++ Class problem

 
0
  #4
Nov 24th, 2008
I don't understand what you are trying to do - but my suggestion was to write a constructor for your B class like this
  1. B::B() {
  2. ob = new A(5);
  3. }
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 delete A; in B's destructor.

Hope this helps.
Last edited by mrboolf; Nov 24th, 2008 at 4:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 392
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 72
StuXYZ StuXYZ is offline Offline
Posting Whiz

Re: Simple C++ Class problem

 
1
  #5
Nov 24th, 2008
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
  1. class A
  2. {
  3. private:
  4. int value;
  5. public:
  6. A(int); // constructor taking a value
  7. };
  8.  
  9. class B
  10. {
  11. private:
  12. A obj;
  13. public:
  14. B(); // Default Constructor
  15. };
  16.  
  17. A::A(int v) : value(v)
  18. {}
  19.  
  20. B::B() : Obj(5)
  21. {}
  22.  
  23.  
  24. int
  25. main()
  26. {
  27. A ob(8);
  28. B bItem;
  29. }

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.

  1. class B
  2. {
  3. private:
  4. A obj;
  5. A secondObj;
  6. public:
  7. B(int,int); // Constructor
  8. };
  9.  
  10. B::B(int a,int b) :
  11. Obj(a),SecondObj(b)
  12.  
  13. int
  14. main()
  15. {
  16. B bItem(3,4); // Make Obj(3) and secondObj(4)
  17. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC