Does C++ say that a default constructor needs to be generated by the
compiler even if a class has the default constructor defined ? Say for
eg. there is a class
class A
{
int i;
A()
{
i=0;
}
};
class B
{
int i;
void foo()
{
i=0;
}
};
In this case will the compiler generate its own default constructor for
class A like in the case of class B and also create the one defined
in the class ? If yes, then what is the functionality of this compiler
generated constructor. 4 3136
"Tapeesh" <ta*****@gmail. com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com... Does C++ say that a default constructor needs to be generated by the compiler even if a class has the default constructor defined ? Say for eg. there is a class
<<snip>>
No. Once you supply a consstructor the compiler will not provide one.
--
Gary
"Tapeesh" <ta*****@gmail. com> wrote in message
news:11******** *************@l 41g2000cwc.goog legroups.com Does C++ say that a default constructor needs to be generated by the compiler even if a class has the default constructor defined ? Say for eg. there is a class
class A {
int i; A() { i=0; } };
class B { int i; void foo() { i=0; } };
In this case will the compiler generate its own default constructor for class A like in the case of class B and also create the one defined in the class ? If yes, then what is the functionality of this compiler generated constructor.
The compiler will NOT generate a default constructor if you define ANY
constructor for a class. Consider for example:
class A
{
int i;
public:
A(int n)
{
i=n;
}
};
This doesn't have a default constructor. Thus if you write:
int main()
{
A a;
}
then it won't compile because
A a;
requires a default constructor and class A does not have one. If you change
it to, say,
A a(5);
then it compiles without a problem.
Incidentally, your original class declaration:
class A
{
int i;
A()
{
i=0;
}
};
has a private constructor, so
A a;
won't compile. You need to make the constructor public.
--
John Carson
John Carson wrote: "Tapeesh" <ta*****@gmail. com> wrote in message news:11******** *************@l 41g2000cwc.goog legroups.com Does C++ say that a default constructor needs to be generated by
the compiler even if a class has the default constructor defined ? Say
for eg. there is a class
class A {
int i; A() { i=0; } };
class B { int i; void foo() { i=0; } };
In this case will the compiler generate its own default constructor for class A like in the case of class B and also create the one defined in the class ? If yes, then what is the functionality of
this compiler generated constructor. The compiler will NOT generate a default constructor if you define
ANY constructor for a class. Consider for example:
class A { int i; public: A(int n) { i=n; } };
This doesn't have a default constructor. Thus if you write:
int main() { A a; }
then it won't compile because
A a;
requires a default constructor and class A does not have one. If you
change it to, say,
A a(5);
then it compiles without a problem.
Incidentally, your original class declaration:
class A { int i; A() { i=0; } };
has a private constructor, so
A a;
won't compile. You need to make the constructor public.
-- John Carson
What i wanted to ask is that when the compiler compiles a code at that
time while creating an object model for the source code, does the
compiler generate some default constructor even if some constructor
(default or otherwise) has been defined for the class?
"Tapeesh" <ta*****@gmail. com> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com What i wanted to ask is that when the compiler compiles a code at that time while creating an object model for the source code, does the compiler generate some default constructor even if some constructor (default or otherwise) has been defined for the class?
The answer has already been given twice. NO.
--
John Carson This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: earl |
last post by:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix;
};
temp::foo(char p, char o, char m = matrix )
|
by: inquirydog |
last post by:
Hi-
I am using xml to hold configuration data for a project, and
using
schema to define what the configuration file should look like. I
wanted to
get some advice on an intelligant way to specify and create a default
config
file. In other words, when the developer puts a new element in the
schema
|
by: ctick |
last post by:
When defining a clas and no constructor and destructor provided, compiler
generates both.
What're the need for this since they do nothing as to
constructing/destructing an obejct.
What's happening in constructor/destructor if they both are defaulted and
empty?
Thanks!
|
by: Gunnar G |
last post by:
If I don't define a default constructor for my class "Foo", I still get one
from the compiler. But if I define a constructor that takes an argument, I
don't get the default constructor, why?
I understand that this is a rule in the language, but what is the motivation
of this rule?
|
by: Giojo |
last post by:
Hello guys!
I have a problem with vistual studio 2005, it's a strange problem...
I have a form, I choose some properties to change so in *.designer.cs
there are the members I changed, only these..
Afterward I inherited my form to make another form, putting there some
default member, so
my B inherits my A form
but B has default members (default for visual studio) that A has
personalized, so in B.designer.cs aren't the members definitions...
| |
by: ali |
last post by:
Hi,
I was reading on inheritance and constructors on a text, and the
author was saying that its a good practice to include a default
constructor even if we have overloaded constructors. I don't
understand why this would be necessary.
Any ideas on the matter? Is it just for convenience or is this
"really" useful style?
|
by: Zytan |
last post by:
I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially...
|
by: JohnQ |
last post by:
Are a default constructor, destructor, copy constructor and assignment
operator generated by the compiler for a struct if they are not explicitely
defined?
I think the answer is yes, because "there is no difference between a struct
and a class except the public/private access specification" (and a few minor
other things). When I create a class, I always start by declaring the
default constructor, copy constructor and assignment operator...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |