473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Initializing class member variable with reference


Hi all,

I need to pass reference to a class via constructor,
to initialize its member variable. This is the code simplified :
class Logger{

public :
Logger(string filename);
};

class Destination{

public :
Destination(Logger &log)

private:
Logger mylogger;
};

int main(int argc, char* argv[]){

Logger logger("logger.config");
Destination dest(logger);

}
In this case, Logger class doesn't have no-args constructor.
How can I pass (by reference ) Logger instance I create on
main function? By now, I do it by defining member variable
mylogger as reference, such this :

private :
Logger& mylogger

and I initialize it using initialization list in constructor
Destination::Destination(Logger& log):mylogger(log){}

Is the way I did it correct ? Or any side effect that I have to
take care ?
Thanks in advance.

Nov 22 '05 #1
2 2613
chris wrote:
I need to pass reference to a class via constructor,
to initialize its member variable. This is the code simplified :
class Logger{

public :
Logger(string filename);
Better:

Logger(string const& filename);
};

class Destination{

public :
Destination(Logger &log)

private:
Logger mylogger;
Shouldn't this be

Logger& mylogger;

???
};

int main(int argc, char* argv[]){

Logger logger("logger.config");
Destination dest(logger);

}
In this case, Logger class doesn't have no-args constructor.
How can I pass (by reference ) Logger instance I create on
main function? By now, I do it by defining member variable
mylogger as reference, such this :

private :
Logger& mylogger
That's better.

and I initialize it using initialization list in constructor
Destination::Destination(Logger& log):mylogger(log){}

Is the way I did it correct ? Or any side effect that I have to
take care ?


No, this seems fine.

The main thing you need to look for is "dangling references" that
can occur when objects retain references to other objects, while
the latter objects have been destroyed. That is not the case in
the presented code: 'dest' gets destroyed before 'logger'.

V
Nov 22 '05 #2
Looks okay to me. Your syntax looks fine.
I can't think of any problems with the way your using it since when
logger goes out of scope so does dest.

Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, I have a class that contains a large number of discrete pieces of state information. Any combination of these member variables might be valid for a given object. Any given member...
5
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but...
5
by: amparikh | last post by:
I have some test code which demonstrates the problem. I know I could solve this by just returning a pointer, but I better use a reference. In real code, what I actually want to return is a...
17
by: Calle Pettersson | last post by:
Coming from writing mostly in Java, I have trouble understanding how to declare a member without initializing it, and do that later... In Java, I would write something like public static void...
6
by: alacrite | last post by:
If I have this situation class X { Z z; Y y; }; Class X has two objects of type Z and Y. How do I initialize z and y with non default constructors?
2
by: eriwik | last post by:
Given a simple class like class test { private: size_t size_; int* data_; public: test(size_t s) : size_(s), data_(new int { /* ... */ };
6
by: Grey Alien | last post by:
class A { public: A(const B& ref); private: static B& b ; }; How may b be initialized ?
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
16
by: Ray | last post by:
Hi all, After many years of C, I thought I'd move to C++ recently. I think because I think in C, I'm coming to a misunderstanding of something. I've created a class foo which contains a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.