Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Constructors

Reply
Thread Tools

Constructors

 
 
Andrea Crotti
Guest
Posts: n/a
 
      10-27-2010
Now, suppose that in

--8<---------------cut here---------------start------------->8---
class X
private:
ostream& out;
Other obj;
--8<---------------cut here---------------end--------------->8---

But object has a constructor that takes "out" as a parameter, which in
this step is of course not initialized.

How can I solve it? With a pointer maybe (also a reference would have
the same problem)?

Or maybe this should not happen in general?
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      10-27-2010
On 10/27/10 10:13 PM, Andrea Crotti wrote:
> Now, suppose that in
>
> --8<---------------cut here---------------start------------->8---
> class X
> private:
> ostream& out;
> Other obj;
> --8<---------------cut here---------------end--------------->8---
>
> But object has a constructor that takes "out" as a parameter, which in
> this step is of course not initialized.
>
> How can I solve it? With a pointer maybe (also a reference would have
> the same problem)?
>
> Or maybe this should not happen in general?


Your questions doesn't make a lot of sense, why don't you post real code?

If a reference member must be initialised in constructors.

--
Ian Collins
 
Reply With Quote
 
 
 
 
Yakov Gerlovin
Guest
Posts: n/a
 
      10-27-2010
Why not to define op<< for 'Other' class instead of passing a
reference to 'ostream' in constructor?
In this case your 'X' class can control how the instance of 'Other' is
printed.

References must be initialized in initializer list, so your X class
also has to define a constructor that takes 'ostream':

class Obj
{
ostream& out;
public:
Obj(ostream& out_) : out(out_) { out << "Obj::Obj" << endl; }
};

class X
{
ostream& out;
Obj o;
public:
X(ostream& out_) : out(out_), o(out_) { out << "X::X=" << endl; }
};
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reg constructors/copy constructors inheritance srp113 C++ 3 02-17-2009 04:01 PM
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57