Solved

I want to initialize a member of a class when its parent is initialized

Posted on 2002-05-17
3
202 Views
Last Modified: 2010-04-02
//
//  I want a class One to contain an instance of class Two and pass a variable to
//  that instance of class Two upon initialization.  
//
//  In other words, when I initialize AclassOneVar, it should create and initialize aaa
//  which is a class Two variable.
//
//  What is wrong with this code?
//
//  Thanks,
//     Ken


#include <iostream>

class Two {
 
public:
  Two(int b );  
  inline int ShowB() { return b_; } ;

private:
  int b_ ;

} ;

class One {
 
public:
  One(int a );
  ~One();
  inline int ShowA() { return a_; } ;
  inline Two *ShowAAA() { return aaa; } ;
  inline int ShowAAA_B() { return aaa->ShowB() ; } ;

private:
  int a_ ;
  Two *aaa ;

} ;

Two::Two(int b ): b_(b) {
}

One::One(int a ) {
  a_ = a ;
  Two *aaa = new Two( a_+100 ) ;
}

One::~One() {
  //  delete aaa;                                   //  Seg faults if left uncommented
}

main() {

  Two AclassTwoVar(3);

  One AclassOneVar(13);
  Two *aa = new Two(19) ;

  cout << AclassTwoVar.ShowB() << endl ;             //  prints out 3 as expected
  cout << aa->ShowB() << endl ;                      //  prints out 19 as expected
  cout << AclassOneVar.ShowAAA_B() << endl ;         // prints out garbage should print 113
  cout << AclassOneVar.ShowAAA()->ShowB() << endl ;  // prints out garbage should print 113

  delete aa ;
}

0
Comment
Question by:klopter
  • 2
3 Comments
 

Author Comment

by:klopter
ID: 7017730
My mistake.  In the One::One() initialiation, there is a
"Two *" which does not belong.  When I take that out I get
the behaviour I want.


I will this question up to see if anyone has a better solution.  ie. One that does not involve pointers and
new().

Thanks,
  Ken
0
 
LVL 3

Accepted Solution

by:
GGRUNDY earned 150 total points
ID: 7017778
You don't need pointers & stars.
Be sure that a_ is declared before aaa in class One.
(the order in which you declare the members is important).

class One {
 
public:
 One(int a );
 ~One();
 inline int ShowA() { return a_; } ;
 inline Two& ShowAAA() { return aaa; } ;
 inline int ShowAAA_B() { return aaa.ShowB() ; } ;

private:
 int a_ ;
 Two aaa ;

} ;

// ************************************
// then you just construct class One as below

One::One(int a ):a_(a),aaa(a_+100)
{
}
0
 

Author Comment

by:klopter
ID: 7017782
You get 150 points for an ampersand :)

That is exactly what I wanted, but I never put in that ampersand in   "inline Two& ".

I only went to the pointer scheme because I couldn't make this work.

Thanks,
  Ken
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question