delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/30/11:17:46

Newsgroups: comp.os.msdos.djgpp
From: "A. Jans-Beken" <jabe AT oce DOT nl>
Subject: Re: Easy C++ stuff... I hope, anyway.
Message-ID: <34D1EC54.5105@oce.nl>
Sender: news AT oce DOT nl (The Daily News @ nntp01.oce.nl)
Organization: Océ-Nederland B.V.
References: <6aolmn$3vs AT nnrp3 DOT farm DOT idt DOT net>
Mime-Version: 1.0
Date: Fri, 30 Jan 1998 15:05:56 GMT
Lines: 42
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Chia wrote:
> 
> 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
> 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);

This IMO is no good C++ code ^^^^
You can use:
    a class_a_instance
but not
    a class_a_instance(x)

Why?
During the definition of class B you still have NOT an instance of B.
Therefore you can not initialize a member of B. You have to initialize A
in your constructor for B.
The constructor for B should be:
   b::b(int x): a(x) { /* init other members of B here */ }

>   // 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;
> }

- Raw text -


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