delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/29/06:37:20

From: G DOT DegliEsposti AT ads DOT it
To: djgpp AT delorie DOT com
Message-ID: <C125659B.003064B8.00@vega.ads.it>
Date: Thu, 29 Jan 1998 09:54:09 +0100
Subject: Re: Easy C++ stuff... I hope, anyway.
Mime-Version: 1.0




>I've got two classes...  Both of them have lots of constructive arguments,
>and I want to have one of them be contained from the other one.  Here's
what
This is not djgpp specific, it was better posting to comp.lang.c++...
anyway...

>I mean:
>
>class a {
>public:
> a(int b) {
>  x = b;
> }
>protected:
> int x;
>}
>
>class b {
>public:
> b(int x) {
>  // normally, I would just do this:
>  //  a class_a_instance(x);
>  // but that doesn't seem to work.
>  // any ideas on how to call class a's constructor
>  // if it's part of another class?
>  // thanks!  chia AT top DOT net
> }
>protected:
> a class_a_instance;
>}

if you declare a member of class "a" you must provide a default constructor
for
that class (i.e. a constructor without arguments).

you can try one of these:

1) class b : public a {
    b(int x) : a(x) { ...

2) a *class_a_instance;
    b(int x) { class_a_instance = new a(x); ...

of course c++ allows other MAXINT more solutions, but two is a beginning
:-)

ciao
  Giacomo





- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019