GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-May-2008, 03:40
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 22
vicky_brsh has a little shameless behaviour in the past

Initialising a Reference variable present in a class


Hi All,

As far as i know we have to initialise a reference when we declare it.

But, how can we initialise a variable that is present as a member of a class ?

Can any one help to solve this problem ?

Thanks and Regards
Vikram
  #2  
Old 16-May-2008, 07:12
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 452
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Initialising a Reference variable present in a class


Do you know what a class constructor is? This is the method that fires when your class is created. That is the best place to put initialization routines in. For example:
CPP / C++ / C Code:
class Example
{ private:
    int x;
  public:
    Example();
    ~Example();
};

// Constructor
Example::Example()
{ 
  // Initialize x to 10  
  x = 10;
}

// Destructor
Example::~Example()
{
  // Free any class memory that was allocated with new or malloc/calloc here
}
  #3  
Old 16-May-2008, 07:48
vicky_brsh vicky_brsh is offline
New Member
 
Join Date: Dec 2007
Posts: 22
vicky_brsh has a little shameless behaviour in the past

Re: Initialising a Reference variable present in a class


Hi ,

Oops sorry there was a mistake in the question .

What i wanted to know is how to initialise a Reference variable present as a member variable in the class.

whether it is right to have a reference variable as a member variable in the class or not ?


Thanks and Regards
Vikram
  #4  
Old 16-May-2008, 07:52
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,648
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Initialising a Reference variable present in a class


Quote:
Originally Posted by vicky_brsh
As far as i know we have to initialise a reference when we declare it.
That is absolutely correct.
Quote:
Originally Posted by vicky_brsh
how can we initialise a variable that is present as a member of a class ?
You declare it (and initialize it) in the class constructor(s). (Where else could you do it?). Syntax and semantics are exactly the same as for any other reference variable.

How about giving an example of exactly what you want to do and what you don't understand about it? Be specific.

Regards,

Dave
Last edited by davekw7x : 16-May-2008 at 08:29.
 
 

Recent GIDBlogToyota - 2008 July Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Two-Tier data dissemination code installation problem nidhibansal1984 Computer Software Forum - Linux 6 16-Sep-2007 10:13
Hard drive/CPU Diagnoses Issues binarybug Computer Hardware Forum 1 22-Jan-2007 19:23
Message Class TransformedBG C++ Forum 5 29-Nov-2006 21:28
C++ class -- Please help vnca_1 C++ Forum 3 14-Jun-2006 12:31
a tester class and then some. postage Java Forum 1 06-May-2006 15:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 00:29.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.