Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2002-05-17
3
Medium Priority
?
231 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

926 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