473,387 Members | 3,750 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.

cTor argument byVal or byRef

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?
Aug 2 '08 #1
9 1783
On 2 Aug., 13:13, ManicQin <Manic...@gmail.comwrote:
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
Aug 2 '08 #2
On Aug 2, 2:41*pm, peter koch <peter.koch.lar...@gmail.comwrote:
On 2 Aug., 13:13, ManicQin <Manic...@gmail.comwrote:
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

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

both of them works, what is the difference?
Aug 2 '08 #3
On Aug 2, 2:55*pm, ManicQin <Manic...@gmail.comwrote:
both of them works, what is the difference?
Ah ok it's the same.
Great thanks!
Aug 2 '08 #4
On 2008-08-02 07:41:43 -0400, peter koch <pe***************@gmail.comsaid:
>
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.
That is, you can temporarily construct an object; you don't construct classes.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Aug 2 '08 #5
On 2 Aug., 14:07, Pete Becker <p...@versatilecoding.comwrote:
On 2008-08-02 07:41:43 -0400, peter koch <peter.koch.lar...@gmail.comsaid:
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.

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
Aug 2 '08 #6
In article
<ea**********************************@k30g2000hse. googlegroups.com>,
ManicQin <Ma******@gmail.comwrote:
...
T const& val? not const T& val?

both of them works, what is the difference?
The first is part of a consistent style for using const, the second not:

<http://codecraft.pool-room.com/Cpp/const-correctness-3.html>
Aug 2 '08 #7
ManicQin wrote:
>

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
Aug 2 '08 #8
On Aug 2, 1:13 pm, ManicQin <Manic...@gmail.comwrote:
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.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '08 #9
On Aug 2, 4:10 pm, "Bo Persson" <b...@gmb.dkwrote:
ManicQin wrote:
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.
One of them is used by the Guru you most respect. Which one
depends on which Guru you most respect, of course. (Note,
however, that even Gurus have been known to change their mind.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 2 '08 #10

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

Similar topics

8
by: Sandy | last post by:
Hello! Help!!!! I have ten zillion books that attempt to describe the difference between ByVal and ByRef and none of them are clear to me. I have gathered that ByVal makes a copy and ByRef...
2
by: Zoury | last post by:
Hi there! I'm using a COM component from my C# application and I can't get it to work properly. I get a COMException (Invalid procedure call or argument) on this line : //***...
7
by: Hei | last post by:
Hi, i know the difference of ByRef and ByVal, in case if use byref or byval don't affect the result which one should prefer? (less memory use, better performance ....issue) thx
4
by: Carlos Gomez | last post by:
In VB6 the default for passing variables was ByRef. It was faster and used less memory. Why did MS changed that? Are there any advantages using ByVal over ByRef? (other than ByVal impeding you from...
14
by: Robin Tucker | last post by:
Although I've been working on this project for 8 months now, I'm still not sure of the difference between ByVal and ByRef. As most objects in VB are reference types, passing ByVal I've discovered...
14
by: Niklas | last post by:
Hi What I have learned is that a variable is just a reference when dealing with Objects. Are you supposed to use ByVal or ByRef in functions? They produce the same result or have I missed...
4
by: Warren Sirota | last post by:
Hi, Please let me know if I am interpreting this correctly. I've done a little testing of the difference between passing parameters byVal and byRef, and the results were slightly non-intuitive,...
13
by: Shannon Richards | last post by:
Hello: I have a problem using ByRef arguments with Option Strict ON. I have built a generic sub procedure "ChangeValue()" to change the value of an argument if the new value is not the same as the...
7
by: barrett bonden | last post by:
Is there any way to pass parameters to a function and simply know there will get there without the silly (C like ) complexity of worring about byval and or perhaps byref ? (Why bother to...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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.