Sunday, August 18, 2013

Initializing member references to dummy member variables

Newsgroup: comp.lang.c++

Subject: Initializing member references to dummy member variables

From: "K. Frank" <kfrank29.c@...>

Date: Sun, 11 Aug 2013 20:29:56 -0700 (PDT)



Hello Group!



Suppose I have a class that has a member reference

variable, and sometimes I want to initialize it,

and sometimes I don't. How much sense does it make

to initialize it to a dummy member variable (to shut

the compiler up)?



As a concrete example, let's say I have a class that

is constructed with a value of or reference to some kind

of handle, but the handle might be a number or it might

be a string:



struct Handle {

Handle (int iHandle) :

iHandle_(iHandle),

sHandle_(dummy),

useIHandle_(true)

{}

Handle (const std::string& sHandle) :

iHandle_(0),

sHandle_(sHandle),

useIHandle_(false)

{}

const int iHandle_;

const std::string& sHandle_;

bool useIHandle_;

std::string dummy_;

}



The point is that the member reference variable sHandle_

is supposed to be initialized (in the constructors'

initialization lists), whether or not it's actually

going to be used. The constructor that takes an int

argument doesn't have any std::strings floating

around with which to initialize sHandle_, hence the

introduction of the member variable dummy_.



Is this a reasonable approach? Is there an established

idiom for doing this kind of thing?





Thanks for any wisdom.





K. Frank







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?63071-Initializing-member-references-to-dummy-member-variables&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment