473,398 Members | 2,525 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,398 software developers and data experts.

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.
Jan 20 '08 #1
1 1431
On Jan 20, 12:09 am, zraj...@gmail.com 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);
}
Jan 20 '08 #2

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

Similar topics

4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
1
by: SK | last post by:
Hi all, I have a doubt in C++ Templates by Nicolai M. Josuttis. On Page 17 there is a line "In general, it is a good idea not to change more than necessary when overloading function templates....
7
by: Hendrik Schober | last post by:
Hi, I have a problem, that boils down to the following code: #include <iostream> #include <typeinfo> class Test1 {}; class Test2 {}; class Test3 {};
2
by: Malcolm Smith | last post by:
I have this functor: struct DeleteIterObject { template< typename T > void operator()(const T* ptr) const { delete ptr; ptr = NULL; }
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: Kalle Rutanen | last post by:
Here is a short code. I can't see why the template specialization does not work (vsnet2003)? Does the standard allow these kind of specializations? template <int i> class B {}; template...
2
by: allan.mcrae | last post by:
I am having trouble with overloading the += operator when template parameters are used. I have a class holding an array (called "derived" in the following example) which derives from a base class...
3
by: Szabolcs | last post by:
Consider the attached example. When I try to compile it (with g++ 4.1.2), I get the error message error: no matching function for call to 'fun(A<int>&)' However, if I do not use templates, it...
7
by: matthias.neubauer | last post by:
I have problems understanding how overloading of function templates works. Consider first the following code without any function templates ... int foo(const char *& c) { return 0; }
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.