Solved

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

Posted on 2002-05-17
3
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.

624 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