473,397 Members | 1,972 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,397 software developers and data experts.

wonder about constructors?

Tom
I was reading Bjourne's book and wonder about constructors?

If I have a class

template<class T> class Vector {
public:
explicit Vector(size_t n);
}

Does a default constructor get generated by the compiler?
Jun 3 '06 #1
8 1628
Tom wrote:
I was reading Bjourne's book and wonder about constructors?

If I have a class

template<class T> class Vector {
public:
explicit Vector(size_t n);
}

Does a default constructor get generated by the compiler?


No.

int main()
{
Vector<int> v; // illegal
}

Note that "explicit" has nothing to do with it.
Jonathan

Jun 3 '06 #2
Tom wrote:
I was reading Bjourne's book and wonder about constructors?

If I have a class

template<class T> class Vector {
public:
explicit Vector(size_t n);
}

Does a default constructor get generated by the compiler?


No. Whenever you define any constructor for a class (or class template), the
compiler will not generate a default constructor for it.

Jun 3 '06 #3

"Tom" <t@please.noemail> wrote in message
news:01c68751$fb7cd200$0b01a8c0@n56l987...
| I was reading Bjourne's book and wonder about constructors?
|
| If I have a class
|
| template<class T> class Vector {
| public:
| explicit Vector(size_t n);
| }

semicolon missing

|
| Does a default constructor get generated by the compiler?

No, but whats preventing you from providing one? Why should the compiler be
allowed to guess about your needs and requirements? Same goes for both the
copy ctor and the assignment operator as well.

#include <vector>

template< typename T >
class Vector
{
std::vector< T > vt;
public:
Vector() : vt() { }
Vector(unsigned sz) : vt(sz) { }
~Vector() { }
};

Jun 4 '06 #4
Salt_Peter wrote:

"Tom" <t@please.noemail> wrote in message
news:01c68751$fb7cd200$0b01a8c0@n56l987...
| I was reading Bjourne's book and wonder about constructors?
|
| If I have a class
|
| template<class T> class Vector {
| public:
| explicit Vector(size_t n);
| }

semicolon missing

|
| Does a default constructor get generated by the compiler?

No, but whats preventing you from providing one? Why should the compiler
be allowed to guess about your needs and requirements?
Because it does that too if you don't define your own constructor.
Same goes for both the copy ctor and the assignment operator as well.


They are different. The default constructor won't be generated by the
compiler if you define _any_ constructor. The others will _always_ be
there, no matter what you define or don't define.

Jun 4 '06 #5
In message <e5*************@news.t-online.com>, Rolf Magnus
<ra******@t-online.de> writes
Salt_Peter wrote:

"Tom" <t@please.noemail> wrote in message
news:01c68751$fb7cd200$0b01a8c0@n56l987...
| I was reading Bjourne's book and wonder about constructors?
|
| If I have a class
|
| template<class T> class Vector {
| public:
| explicit Vector(size_t n);
| }

semicolon missing

|
| Does a default constructor get generated by the compiler?

No, but whats preventing you from providing one? Why should the compiler
be allowed to guess about your needs and requirements?


Because it does that too if you don't define your own constructor.
Same goes for both the copy ctor and the assignment operator as well.


They are different. The default constructor won't be generated by the
compiler if you define _any_ constructor. The others will _always_ be
there, no matter what you define or don't define.

Moreover, whenever it makes sense semantically it's _better_ to let the
compiler generate them than do it yourself, since the compiler won't
forget to initialise or assign every member, but the programmer might.
#include <vector>
template< typename T >
class Vector
{
std::vector< T > vt;
public:
Vector() : vt() { }
Pointless.
Vector(unsigned sz) : vt(sz) { }
What if std::vector::size_type is larger than unsigned?
~Vector() { }
Pointless.
};


--
Richard Herring
Jun 7 '06 #6
Richard Herring wrote:
Moreover, whenever it makes sense semantically it's _better_ to let the
compiler generate them than do it yourself, since the compiler won't
forget to initialise or assign every member, but the programmer might.

The copy constructor and copy-assignment operators are one thing,
but pray tell me what the "compiler generated default constructor"
is really doing for you?

The stupidity of C++ is that certain types have busted default
initialization (not so much that default initialization is busted
but the stupid language doesn't require it to be performed in
all cases).
Jun 12 '06 #7

"Ron Natalie" wrotes:
Moreover, whenever it makes sense semantically it's _better_ to let the
compiler generate them than do it yourself, since the compiler won't
forget to initialise or assign every member, but the programmer might.

The copy constructor and copy-assignment operators are one thing,
but pray tell me what the "compiler generated default constructor"
is really doing for you?

The stupidity of C++ is that certain types have busted default
initialization (not so much that default initialization is busted
but the stupid language doesn't require it to be performed in
all cases).


I think the thing that is broken is the terminology. A "constructor" sounds
like a funny name for a function that does something or other, presumably
something one wants done for him. For an ordinary class, the default ctor
does the same thing as is done when an int is declared, that is, nothing.
Space is allocated and that is the sum total of what happens. So C++ grants
a special name to a nonentity.

Jun 12 '06 #8

Rolf Magnus wrote:
Tom wrote:
I was reading Bjourne's book and wonder about constructors?

If I have a class

template<class T> class Vector {
public:
explicit Vector(size_t n);
}

Does a default constructor get generated by the compiler?


No. Whenever you define any constructor for a class (or class template), the
compiler will not generate a default constructor for it.


Whenever you declare, rather than define, any constructor ...

The most obvious example of declaring but not defining a constructor
that springs to mind is disabling copying by declaring the copy
constructor (and assignment operator, but that has no effect either way
on the compiler generated default constructor) private.

// Don't need any special initialisation so I'll
// rely on the compiler generated default constructor
class foo
{
public:
// stuff (but no constructors) ...
private:
// Disable copying
// No definition is provided anywhere for these two
foo(const foo&);
foo& operator=(const foo&);

// other stuff (but no constructors) ...
};

int main()
{
foo f; // Error: no default constructor available
}

Gavin Deane

Jun 12 '06 #9

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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
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
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.