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

question about default constructor

consider the following code:
class A {
public:
A(int i) : i(i) {
}

private:
int i;
};
int main()
{
A a;

return 0;
}

The code cannot compile because there is no default constructor of
class A, but the book says that "the compiler will generate the default
constructor if you don't define it"

What's wrong ?

Jul 23 '05 #1
4 1090

"July" <ju**********@yahoo.com> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
consider the following code:
class A {
public:
A(int i) : i(i) {
}

private:
int i;
};
int main()
{
A a;

return 0;
}

The code cannot compile because there is no default constructor of
class A, but the book says that "the compiler will generate the default
constructor if you don't define it"

What's wrong ?


I beleive it's actually, "the compiler will generate a defautl constructor
if you don't define any constructor".
Jul 23 '05 #2
* July:

the book says that "the compiler will generate the default
constructor if you don't define it"


Which book is that (so that others can avoid it)?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3
> consider the following code:
class A {
public:
A(int i) : i(i) {
}

private:
int i;
};
int main()
{
A a;

return 0;
}

The code cannot compile because there is no default constructor of
class A, but the book says that "the compiler will generate the default
constructor if you don't define it"

What's wrong ?


The standard goes "If there is no user-defined constructor for class X, a
default constructor is implicitly declared."

Well, you declared (and defined) a constructor indeed, so the compiler won't
synthesize the default constructor for you. Not that the quote above didn't
say "if there is no user-defined DEFAULT constructor...".

Regards,
Ben
Jul 23 '05 #4
July wrote:
consider the following code:
class A {
public:
A(int i) : i(i) {
}

private:
int i;
};
int main()
{
A a;

return 0;
}

The code cannot compile because there is no default constructor of
class A, but the book says that "the compiler will generate the default
constructor if you don't define it"

What's wrong ?


In this case you will **not** have a default constructor generated by
the compiler since you have defined your own paramterised constructor.
the book says that "the compiler will generate the default constructor if you don't define it"


Probably the next sentence/chapter in the book would have been that
"the compiler generates a default constructor if and only if you do not
have any constructor declared/defined in your class".

Jul 23 '05 #5

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

Similar topics

8
by: Simon | last post by:
Hi everyone, Quick question: If I don't use base() in a subclass's construcor, will the base classes constructor be called at any point. It's just, I would have thought that the base class...
3
by: puzzlecracker | last post by:
I think I saw some erratic results with different (some outdated) compilers using a conventional inheritence model. class A { //details omitted A(){...} A(const A&){...} operator=(const A...
3
by: Z D | last post by:
Hello, I've created a winform user control that, at some point in the default constructor, looks for a specifc file. When I try to load the user control to my winform's form during design time...
3
by: Mike | last post by:
Hello In the following code,why are there 2 different Class1's.Is one a class definition and the other a function? namespace Music { public class Class1 { public Class1() {
4
by: gw7rib | last post by:
Suppose I have the following code fragment: class base { private: int m; public: base(int x) { m = x; } };
3
by: Alex Howlett | last post by:
Stroustrup page 663 says this: valarray<floatv1 (1000); // 1000 elements with value float()==0.0F But when I run this program: -------------------------------------- #include <valarray>...
3
by: bayan1 | last post by:
/*The following program have the following output. write the necessary code to make it works? Code :*/...
11
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <vector> using namespace std; class Test { public:
8
by: hill.liu | last post by:
Hi, I stuck into this problem that I can't figure it out. Here is the class definition: class ctest { public: ctest(void) { cout << "ctest default constor" << endl; }; ctest(ctest& c) {...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
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,...
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
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 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.