Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > class constructor and members

Reply
Thread Tools

class constructor and members

 
 
imutate@hotmail.co.uk
Guest
Posts: n/a
 
      09-25-2006
Some questions about ctors and class members

Is v private in the following ? If it is why put the declaration at
the top ? Is there any difference to putting it in the private section
?
The reference to v() defines the constructor, it will call v's
constructor, right ?

class vec
{
std::vector< celement > v;
public:
vec() : v() { }


If you declare an empty constructor like In the following is there any
difference to when it is left out ? Does C++ differ from other
languages in that you do not have to explicitely call the default ctor
from within the "programed" one.

class compos
{
int a;
double b;
public:
compos() { } // <- but there is nothing in it ?

Finally does this last example have the same results as the previous
one ?

class compos
{
int a;
double b;
public:
compos();

compos::compos
{ }

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-25-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Some questions about ctors and class members
>
> Is v private in the following ? If it is why put the declaration at
> the top ? Is there any difference to putting it in the private
> section ?
> The reference to v() defines the constructor, it will call v's
> constructor, right ?
>
> class vec
> {
> std::vector< celement > v;


Here 'v' is private.

> public:
> vec() : v() { }


And here you omit the end of the 'vec' definition for some reason.

>
>
> If you declare an empty constructor like In the following is there any
> difference to when it is left out ?


No.

> Does C++ differ from other
> languages in that you do not have to explicitely call the default ctor
> from within the "programed" one.


I guess I don't understand the question. In C++ at this point you cannot
"call the default ctor from within". There is a proposal on the table to
add this functionality, but it's not going to make it into the language
any time soon.

>
> class compos
> {
> int a;
> double b;


'a' and 'b' are private.

> public:
> compos() { } // <- but there is nothing in it ?


Yep, nothing. Both members are left uninitialised.

>
> Finally does this last example have the same results as the previous
> one ?
>
> class compos
> {
> int a;
> double b;
> public:
> compos();


Missing:

};

>
> compos::compos
> { }


Sure. You just moved the definition outside, you didn't change what
the function does and how it does it.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
 
 
 
imutate@hotmail.co.uk
Guest
Posts: n/a
 
      09-25-2006
Thanks for the answers, just a few things.

Victor Bazarov wrote:
> (E-Mail Removed) wrote:
> > public:
> > vec() : v() { }

>
> And here you omit the end of the 'vec' definition for some reason.
>


You mean the rest of the definitions (if there were any) and the end
bracket ? Sorry I thought you would prefer brevity, it is one less
line for you to read ;}

> > Does C++ differ from other
> > languages in that you do not have to explicitely call the default ctor
> > from within the "programed" one.

>
> I guess I don't understand the question. In C++ at this point you cannot
> "call the default ctor from within". There is a proposal on the table to
> add this functionality, but it's not going to make it into the language
> any time soon.
>


In some other languages if you want to override the default constructor
you have to explicitly call the default constructor otherwise you will
be in big trouble.. nothing will be created. So in C++ it is a bit
strange seeing empty brackets or just some variable set, where normally
there would be a function call. This was why I was asking about the
empty brackets they may be empty but there is still a valid
constructor. So a natural question to ask is: is the default
constructor called at the begining ?

class compos
{
int a;
double b;
public:
compos();
};

compos::compos()
{
// is it called before this:
a = 1;
}

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-25-2006
(E-Mail Removed) wrote:
> Thanks for the answers, just a few things.


> In some other languages if you want to override the default constructor
> you have to explicitly call the default constructor otherwise you will
> be in big trouble.. nothing will be created.


Constructors do NOT create object in C++.
You can't call them. The constructor is called for you by the
implementation in the proper course during the creation of
each object.

You're also abusing the terminology. The "default constructor"
is one that can be called with no arguments. If you do not define
*ANY* constructors, the compiler implicitly generates an empty one
for you. If you want to define your own, there's NOTHING else
for you to do but add the stuff you want done differently.

Constructors for all the base classes and non-static data members
(subobjects) are called as a matter of course as well before the
body of the constructor you define for the class is executed.
Again there is nothing you can to do change this. The only thing
that you can affect is the selection of which constructor by what
arguments are given to the (sub)object creation.


 
Reply With Quote
 
imutate@hotmail.co.uk
Guest
Posts: n/a
 
      09-26-2006

Ron Natalie wrote:
> (E-Mail Removed) wrote:
> > Thanks for the answers, just a few things.

>
> > In some other languages if you want to override the default constructor
> > you have to explicitly call the default constructor otherwise you will
> > be in big trouble.. nothing will be created.

>
> Constructors do NOT create object in C++.


Eh, well what do they do then ?

> You can't call them.


Are you sure you are right ? Just to be pedantic I thought I saw on the
faq that some people did this by calling one constructor from another,
but the faq did say it was a very bad thing to do this, but it did not
say that it was not possible.

> The constructor is called for you by the
> implementation in the proper course during the creation of
> each object.
>
> You're also abusing the terminology. The "default constructor"
> is one that can be called with no arguments.


Confusing, well what would you call it then (the normal constructor?)
look at this object pascal as a comparison:

Constructor TMyObject.Create(source : TMyObject); overload;
begin
Create;
CopyFrom(source);
end;

The Create call calls the underlying constructor.

> If you do not define
> *ANY* constructors, the compiler implicitly generates an empty one
> for you.


OK, is it called the normal constructor ? But if it is empty how is
the object created ?

> If you want to define your own, there's NOTHING else
> for you to do but add the stuff you want done differently.
>
> Constructors for all the base classes and non-static data members
> (subobjects) are called as a matter of course as well before the
> body of the constructor you define for the class is executed.
> Again there is nothing you can to do change this. The only thing
> that you can affect is the selection of which constructor by what
> arguments are given to the (sub)object creation.


And if you leave out those objects then they won't get created
correctly, or something else ?

 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) wrote:
> Ron Natalie wrote:
>
>>(E-Mail Removed) wrote:
>>
>>>Thanks for the answers, just a few things.

>>
>>>In some other languages if you want to override the default constructor
>>>you have to explicitly call the default constructor otherwise you will
>>>be in big trouble.. nothing will be created.

>>
>>Constructors do NOT create object in C++.

>
>
> Eh, well what do they do then ?
>

If they do anything, they initialise the object. For an automatic
variable, a constructor is called with this pointing to an area in
automatic storage (typically the stack) and for a dynamic object, it
points to the memory returned by new.

--
Ian Collins.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) wrote:

>> Constructors do NOT create object in C++.

>
> Eh, well what do they do then ?


They perform initialization.
>
>> You can't call them.

>
> Are you sure you are right ?


Absolutely. The words come straight out of the standard. Constructors
do not participate in name resolution. They can't be called by code.
Any syntax that appears to do so is either illegal or doing something
else.

For instance:
class X {
public:
X() { }
X(int a) {
X();
}
};

The line in the second (non-default) constructor isn't calling anything.
It makes a temporary default constructed temporary object that vanishes
when the expression it is used ends.

> Just to be pedantic I thought I saw on the
> faq that some people did this by calling one constructor from another,
> but the faq did say it was a very bad thing to do this, but it did not
> say that it was not possible.


The FAQ does not say it. It's not just a bad thing, it's not possible.
If you have code that is common to more than one constructor you need
to put it in another member function that can be called.

There are some people who espouse hacks such as placement constructing
an object on top of the one under construction but it is frowned upon
and also involves implementation-specific or undefined behavior.

>
> Confusing, well what would you call it then (the normal constructor?)
> look at this object pascal as a comparison:


I didn't invent the terms. But default constructor is one called with
no arguments whether its generated by the compiler OR one you write.

>
> The Create call calls the underlying constructor.
>
>
> OK, is it called the normal constructor ? But if it is empty how is
> the object created ?
>

It is important for you to realize that the CREATION: be it as
a result of a locally declared variable, a temporary, or dynamic
creation via new embodies two things:
1. Allocation of storage for the object (which you really don't
have any control over other than the context of the way you
create the object, this code is all automagically emitted by the
compiler for you).
2. Initialization, which involves invoking constructors for
class objects and doing other initialization for more primitive
types.

he standard uses the terms "implicitly declared" and "implicitly
defined" for the four functions it will generate for you in certain
circumstances:
a default constructor (if no constructors are declared)
a copy constructor (if no copy constructor is declared)
a copy-assignment operator (if not declared)
a destructor (if not declared)



>
> And if you leave out those objects then they won't get created
> correctly, or something else ?
>


No, if they are class objects, their default constructors (implicit
or explicitly defined) are invoked. If they are not class objects
then rather stupid and arcane initialization rules are invoked
depending on the context (it is a major freaking defect in C++
that default construction of non-class objects isn't always the
same, but too much whining and crying by ex-C programmers ruined
this).
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-26-2006
(E-Mail Removed) wrote:
> Ron Natalie wrote:
>> If you do not define
>> *ANY* constructors, the compiler implicitly generates an empty one
>> for you.

>
> OK, is it called the normal constructor ?


There are four functions that if not explicitly declared are declared
and often defined for you. They are the *default* constructor, the
*copy* constructor, the destructor, and the *copy* assignment operator.
All constructors are "normal". Not one is "more normal" than others.

> But if it is empty how is
> the object created ?


An object is created by allocating storage for it. The object is
initialised by calling a constructor. Both operations are performed
by the C++ run-time, and not by you. You can create an object by
definining it or by using 'new' or by using the syntax that causes
a temporary object to be created. You don't control explicitly what
constructor is called, but you can do a lot to steer the system to
calling the "right" one.

>> If you want to define your own, there's NOTHING else
>> for you to do but add the stuff you want done differently.
>>
>> Constructors for all the base classes and non-static data members
>> (subobjects) are called as a matter of course as well before the
>> body of the constructor you define for the class is executed.
>> Again there is nothing you can to do change this. The only thing
>> that you can affect is the selection of which constructor by what
>> arguments are given to the (sub)object creation.


Right. To clarify, you can control whether certain members are
initialsed or left uninitialised by mentioning them in, or leaving
out of, the constructor initialiser list.

> And if you leave out those objects then they won't get created
> correctly, or something else ?


The process of initialising members of a class is described in most
decent books. What book on C++ are you reading?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      09-26-2006
Victor Bazarov wrote:

> There are four functions that if not explicitly declared are declared
> and often defined for you. They are the *default* constructor, the
> *copy* constructor, the destructor, and the *copy* assignment operator.


Note that declaration of ANY constructor inhibits the implicit
generation of the default constructor. A class needs not have
a default constructor if not default initialized.
 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      09-26-2006

Ron Natalie wrote:
> There are some people who espouse hacks such as placement constructing
> an object on top of the one under construction but it is frowned upon
> and also involves implementation-specific or undefined behavior.



You mean doing this:

class X
{
public:

explicit X( int a );
X ( int a, const char * b )
{
new( this ) X( a );
// do some more
}
};

Not sure doing so would necessarily be undefined. It might depend on
what is the nature of X and I wouldn't recommend actually doing it.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
call base class constructor from derived class constructor Rahul C++ 16 11-07-2007 03:40 PM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
Invoking templatized base class constructor from templatized derived class constructor mrstephengross C++ 5 05-18-2005 07:12 PM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments