cTor argument byVal or byRef

Discussion in 'C++' started by ManicQin, Aug 2, 2008.

  1. ManicQin

    ManicQin Guest

    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?
     
    ManicQin, Aug 2, 2008
    #1
    1. Advertising

  2. ManicQin

    peter koch Guest

    On 2 Aug., 13:13, ManicQin <> wrote:
    > 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
     
    peter koch, Aug 2, 2008
    #2
    1. Advertising

  3. ManicQin

    ManicQin Guest

    On Aug 2, 2:41 pm, peter koch <> wrote:
    > On 2 Aug., 13:13, ManicQin <> wrote:
    >
    >
    >
    > > 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?
     
    ManicQin, Aug 2, 2008
    #3
  4. ManicQin

    ManicQin Guest

    On Aug 2, 2:55 pm, ManicQin <> wrote:

    > both of them works, what is the difference?


    Ah ok it's the same.
    Great thanks!
     
    ManicQin, Aug 2, 2008
    #4
  5. ManicQin

    peter koch Guest

    On 2 Aug., 14:07, Pete Becker <> wrote:
    > On 2008-08-02 07:41:43 -0400, peter koch <> said:
    >
    >
    >
    > > 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
     
    peter koch, Aug 2, 2008
    #5
  6. ManicQin

    Bo Persson Guest

    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
     
    Bo Persson, Aug 2, 2008
    #6
  7. ManicQin

    James Kanze Guest

    On Aug 2, 1:13 pm, ManicQin <> wrote:

    > 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:
    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
     
    James Kanze, Aug 2, 2008
    #7
  8. ManicQin

    James Kanze Guest

    On Aug 2, 4:10 pm, "Bo Persson" <> wrote:
    > 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:
    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
     
    James Kanze, Aug 2, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Waz...
    Replies:
    0
    Views:
    410
    David Waz...
    Jul 4, 2003
  2. Goncalo

    ByVal and ByRef

    Goncalo, Dec 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    6,056
    Dmitriy Lapshin [C# / .NET MVP]
    Dec 4, 2003
  3. Iams

    Byval vs. byref

    Iams, Feb 24, 2005, in forum: ASP .Net
    Replies:
    5
    Views:
    4,787
  4. xamman
    Replies:
    1
    Views:
    339
    Marina Levit [MVP]
    Dec 22, 2006
  5. Rob Meade

    Byref / Byval?

    Rob Meade, Dec 28, 2006, in forum: ASP .Net
    Replies:
    5
    Views:
    752
    Rob Meade
    Dec 28, 2006
Loading...

Share This Page