Hi, I have some questions regarding templated class constructor:
#include <iostream>
using namespace std;
template<typename T>
class Foo
{
public:
explicit Foo(const T& bar): _bar(bar)
{ cout << "In constructor, _bar = " << _bar << '\n'; }
template<typename U>
Foo(const Foo<U>& rhs): _bar(rhs.GetBar())
{ cout << "In converting constructor, _bar = " << _bar << '\n'; }
Foo(const Foo<T>& rhs): _bar(rhs._bar)
{ cout << "In copy constructor, _bar = " << _bar << '\n'; }
T GetBar() const { return _bar; }
private:
T _bar;
};
int main()
{
Foo<int> intFoo1(1), intFoo2(2);
Foo<int> intFoo3(intFoo1);
Foo<double> dblFoo1(intFoo2);
}
A quick test with VC++6 yields the following result:
In constructor, _bar = 1
In constructor, _bar = 2
In copy constructor, _bar = 1
In converting constructor, _bar = 2
Is this behaviour conforming with the standard? It seems that even if I
remove the copy constructor declaration/definition, the converting
constructor will not be used (the compiler-generated copy constructor
will be used instead). If that's the case, can I safely assume that the
converting constructor will never be called for templated class of the
same type?
Thanks! 2 1897
<fe****************@gmail.com> wrote... Hi, I have some questions regarding templated class constructor:
#include <iostream>
using namespace std;
template<typename T> class Foo { public: explicit Foo(const T& bar): _bar(bar) { cout << "In constructor, _bar = " << _bar << '\n'; }
template<typename U> Foo(const Foo<U>& rhs): _bar(rhs.GetBar()) { cout << "In converting constructor, _bar = " << _bar << '\n'; }
Foo(const Foo<T>& rhs): _bar(rhs._bar) { cout << "In copy constructor, _bar = " << _bar << '\n'; }
T GetBar() const { return _bar; }
private: T _bar; };
int main() { Foo<int> intFoo1(1), intFoo2(2); Foo<int> intFoo3(intFoo1); Foo<double> dblFoo1(intFoo2); }
A quick test with VC++6 yields the following result:
In constructor, _bar = 1 In constructor, _bar = 2 In copy constructor, _bar = 1 In converting constructor, _bar = 2
Is this behaviour conforming with the standard?
Yes, AFAICS.
It seems that even if I remove the copy constructor declaration/definition, the converting constructor will not be used (the compiler-generated copy constructor will be used instead).
Yes, that's correct.
If that's the case, can I safely assume that the converting constructor will never be called for templated class of the same type?
Yes, that's the Standard requirement, IIRC. I couldn't find the passage
from the Standard within 5 minutes, though...
V fe****************@gmail.com wrote: Hi, I have some questions regarding templated class constructor:
#include <iostream>
using namespace std;
template<typename T> class Foo { public: explicit Foo(const T& bar): _bar(bar) { cout << "In constructor, _bar = " << _bar << '\n'; }
template<typename U> Foo(const Foo<U>& rhs): _bar(rhs.GetBar()) { cout << "In converting constructor, _bar = " << _bar << '\n'; }
Foo(const Foo<T>& rhs): _bar(rhs._bar) { cout << "In copy constructor, _bar = " << _bar << '\n'; }
T GetBar() const { return _bar; }
private: T _bar; };
int main() { Foo<int> intFoo1(1), intFoo2(2); Foo<int> intFoo3(intFoo1); Foo<double> dblFoo1(intFoo2); }
A quick test with VC++6 yields the following result:
In constructor, _bar = 1 In constructor, _bar = 2 In copy constructor, _bar = 1 In converting constructor, _bar = 2
Is this behaviour conforming with the standard?
Yes.
It seems that even if I remove the copy constructor declaration/definition, the converting constructor will not be used (the compiler-generated copy constructor will be used instead).
Templated copy-ctor, ctor and assignment operator never replace
non-templated ones, so even if you declare them, the compiler will still
generate default ones.
If that's the case, can I safely assume that the converting constructor will never be called for templated class of the same type?
Yes.
Jonathan This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jon Wilson |
last post by:
I have a class which needs to accumulate data. The way we get this data
is by calling a member function which returns float on a number of
different objects of different type (they are all the...
|
by: kalita |
last post by:
template<class T>
class A
{
template<class Y>
A(const A<Y> &)
{
// whatever
}
};
|
by: Dan Huantes |
last post by:
I was presented a problem today where a class had member
variable that was an object of a templated class. The
class wanted to instantiate the object as a private member
variable and call a...
|
by: shaun |
last post by:
Dear all,
I realized an error in a previous post, I reproduce it here because I'm
still not sure how to solve it:
I want to make a templated function which points to one-past-the-end of
a...
|
by: Claudius |
last post by:
Hello,
in my class TopTen I need to define three constructors while only the
last one, the most general in terms of templates, should be sufficient
in my opinion:
template <typename Tnum,...
|
by: Lawrence Spector |
last post by:
How does one call a templated constructor inside of a class when
instantiating an object? I made up a quick sample to demonstrate.
#include <iostream>
class TestClass
{
public:
template...
|
by: alan |
last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a
variable. Something like:
template<class t>
class cell{
t curval;
public:
/*public for debugging only - will be private in...
|
by: eihabisaac |
last post by:
hey everybody
im having some trouble with my assignment
we should write a program that will handle with a templated linkedlist for a bank account to add/delete amount to the account or sort...
|
by: card |
last post by:
Hi everyone,
I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes:
#include...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
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,...
|
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: 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...
|
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...
|
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...
| |