cTor argument byVal or byRef

M

ManicQin

Hello all.

I've templated a simple class like the next:

template <class _T>
class base
{
public:
base(_T newVal):m_data(newVal){}

private:
_T m_data;
};

AFAIK if the cTor receives _t byVal I can pass him numbers and not
just variable
(feel free to correct me but that's the behavior of VS9... and it
sound reasonable)

I want to add another cTor that receives the arg byRef but than my
compiler
is having problems to deduce what cTor to use..

how can I work around it?
 
P

peter koch

Hello all.

I've templated a simple class like the next:

template <class _T>
class base
{
public:
base(_T newVal):m_data(newVal){}

private:
_T m_data;

};

AFAIK if the cTor receives _t byVal I can pass him numbers and not
just variable
(feel free to correct me but that's the behavior of VS9... and it
sound reasonable)

I want to add another cTor that receives the arg byRef but than my
compiler
is having problems to deduce what cTor to use..

how can I work around it?

Most probably you'd want to pass your argument by const reference: T
const& val. This allows you to pass values and also to temporarily
construct the class: if e.g. T is a std::string you can pass a
literal.

/Peter
 
M

ManicQin

Most probably you'd want to pass your argument by const reference: T
const& val. This allows you to pass values and also to temporarily
construct the class: if e.g. T is a std::string you can pass a
literal.

/Peter


Thanks man...
T const& val? not const T& val?

both of them works, what is the difference?
 
P

peter koch

That is, you can temporarily construct an object; you don't construct classes.

You're right, of course. I presumably meant an object of class type
when my fingers took over ;-)

/Peter
 
B

Bo Persson

ManicQin said:
Thanks man...
T const& val? not const T& val?

both of them works, what is the difference?

One of them looks much better than the other. :)

One of them is more consistent than the other.

Unfortunately, not the same one.


Bo Persson
 
J

James Kanze

I've templated a simple class like the next:
template <class _T>

Just a nit, but don't use names starting with an underscore
followed by a capital letter. It's undefined behavior. For
that matter, don't use names starting with an underscore,
period.
class base
{
public:
base(_T newVal):m_data(newVal){}
private:
_T m_data;
};
AFAIK if the cTor receives _t byVal I can pass him numbers and
not just variable (feel free to correct me but that's the
behavior of VS9... and it sound reasonable)

What you mean is that if the parameter is passed by value, you
can use any arbitrary expression as the argument, provided it
has the right type (or can be implicitly converted to the right
type). That's right.
I want to add another cTor that receives the arg byRef but
than my compiler is having problems to deduce what cTor to
use..
Why?

how can I work around it?

It depends on why you want to do it. If nothing else, there's
always:

template< typename T >
class Base
{
public:
enum ByValue { byValue } ;
enum ByReference { byReference } ;
Base( ByValue, T value ) ...
Base( ByReference, T& value ) ...
} ;

I'd need a very, very strong reason before doing something like
that. (I think in close to twenty years experience with C++,
I've needed something like that exactly once.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top