Solved

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

Posted on 2002-05-17
3
206 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

820 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