Solved

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

Posted on 2002-05-17
3
194 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
Comment Utility
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
Comment Utility
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
Comment Utility
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now