Templates, overloading & const



 DEVELOP > c-Plus-Plus > Templates, overloading & const

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: ""
Date: 19 Jan 2008 11:09:40 PM
Object: Templates, overloading & const
Hi,
I have the following code.
struct Object
{
int m_id;
void setId(int const id)
{
m_id = id;
}
};
// version 1
template < class T >
void resetId(T &obj, int const id)
{
obj.setId(id);
}
// version 2
template < class T >
void resetId(boost::shared_ptr < T > const &obj, int const id)
{
obj->setId(id);
}
int main()
{
Object obj1;
resetId(obj1, 1); // calls version 1
boost::shared_ptr < Object > obj2;
resetId(obj2, 2); // This calls version 1 too
}
This is with GCC 3.2.3...
How can I make sure that the call "resetId(obj2, 2)" calls version 2
that is overloaded for shared_ptr objects.
.

User: "Salt_Peter"

Title: Re: Templates, overloading & const 20 Jan 2008 02:06:42 AM
On Jan 20, 12:09 am,
wrote:

Hi,

I have the following code.

struct Object
{
int m_id;
void setId(int const id)
{
m_id = id;
}

};

// version 1
template < class T >
void resetId(T &obj, int const id)
{
obj.setId(id);

}

// version 2
template < class T >
void resetId(boost::shared_ptr < T > const &obj, int const id)

void resetId(boost::shared_ptr< T >& sp, int const id)

{
obj->setId(id);

sp->setId(id);


}

int main()
{
Object obj1;
resetId(obj1, 1); // calls version 1

boost::shared_ptr < Object > obj2;
resetId(obj2, 2); // This calls version 1 too

boost::shared_ptr< Object > bsp(new Object);
resetId(bsp, 2);
note: don't construct a temporary, see boost's notes on shared_ptr


}

This is with GCC 3.2.3...

How can I make sure that the call "resetId(obj2, 2)" calls version 2
that is overloaded for shared_ptr objects.

Any reason you aren't using constructors? After all, the compiler will
generate a default ctor if you don't. And you can supply as many
constructors as you need. Do not ignore ctors in C++, including copy
constructors. In fact, i find its *very* rare that i don't provide
ctors + init list for types. They save a lot of hardship and greatly
enhance the code.
struct Object
{
Object() : m_id(0) { } // default ctor
Object(const int id) : m_id(id) { } // parametized ctor
resetId(int const id) { m_id = id; }
private:
int m_id;
};
int main()
{
Object obj(1);
}
.
User: ""

Title: Re: Templates, overloading & const 30 Jan 2008 06:30:13 PM
The code is just an example piece from the actual problem. I don't
have the luxury of constructors that take just the ID, because the
code for class Object is generated from a kind of IDL where there is
no constructor that just sets the ID value and ID is a member that I
dont define, but generated by the IDL compiler automatically. Hope
that helps.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER