473,803 Members | 2,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

uninitializing constructor

Hello,

I'd like to know whether the compiler can detect a member variable which is
not initialized (on purpose) by the constructor.

Easier with an simple example (the class is supposed to be a fixed point
variable):

class A
{
private:
int my_value;
A (int init) { my_value = init; }
public:
A () {}
A (const A& init) { my_value = init.my_value; }
A& operator = (int scalar) { my_value = scalar << 8; }
...
};

int main (void)
{
int p, q;
A x, y;

p = q;
y = x;
}

The compiler complains about q beeing used but not initialized,
is there a way for it to complain about x too ?

(yes I could put 0 in the constructor, but code size is critical in the
embedded environment I use, and I'm curious too :-)

If of any interest for the answer, I'm using gnu-g++. Before I post to the
gnu newsgroup, I'd like to know if the answer is part of some c++ standards.

(I also tried "A () const {}" but it is refused)

Thanks for any information,

david
Oct 6 '06 #1
4 1801

david wrote:
I'd like to know whether the compiler can detect a member variable which is
not initialized (on purpose) by the constructor.
Not in an implementation-independent way.

Easier with an simple example (the class is supposed to be a fixed point
variable):

class A
{
private:
int my_value;
A (int init) { my_value = init; }
public:
A () {}
A (const A& init) { my_value = init.my_value; }
A& operator = (int scalar) { my_value = scalar << 8; }
...
};

int main (void)
{
int p, q;
A x, y;

p = q;
y = x;
}

The compiler complains about q beeing used but not initialized,
is there a way for it to complain about x too ?

(yes I could put 0 in the constructor, but code size is critical in the
embedded environment I use, and I'm curious too :-)

If of any interest for the answer, I'm using gnu-g++. Before I post to the
gnu newsgroup, I'd like to know if the answer is part of some c++ standards.
It's not. There is nothing in the Standard that requires a diagnostic,
so it is completely implementation dependent.

Best regards,

Tom

Oct 6 '06 #2
>I'd like to know whether the compiler can detect a member variable which is
>not initialized (on purpose) by the constructor.

Not in an implementation-independent way.
[...]
>>
If of any interest for the answer, I'm using gnu-g++. Before I post to the
gnu newsgroup, I'd like to know if the answer is part of some c++ standards.

It's not. There is nothing in the Standard that requires a diagnostic,
so it is completely implementation dependent.
Thanks a lot,

david
Oct 6 '06 #3

david wrote:
Hello,

I'd like to know whether the compiler can detect a member variable which is
not initialized (on purpose) by the constructor.
yes it can, but you'ld have to code that in. For the record, thats not
needed.
rule# 1: always, always intialize your members.
The members get allocated whether you initialize them or not.
>
Easier with an simple example (the class is supposed to be a fixed point
variable):

class A
{
private:
int my_value;
A (int init) { my_value = init; }
public:
A () {}
A (const A& init) { my_value = init.my_value; }
A& operator = (int scalar) { my_value = scalar << 8; }
shouldn't the assignment op here be a conversion ctor instead?
...
};

int main (void)
{
int p, q;
A x, y;

p = q;
y = x;
}

The compiler complains about q beeing used but not initialized,
is there a way for it to complain about x too ?
The compiler is not required to complain about a user-type's members
that are not initialized.
>
(yes I could put 0 in the constructor, but code size is critical in the
embedded environment I use, and I'm curious too :-)
What makes you beleive that the allocation will not take place if you
don't initialize the member variable? The members of an instance are
allocated whether you initialize them or not. Therefore the cost of
initializing the member to 0 or whatever is minimal.
>
If of any interest for the answer, I'm using gnu-g++. Before I post to the
gnu newsgroup, I'd like to know if the answer is part of some c++ standards.

(I also tried "A () const {}" but it is refused)
Your class should use the int list and i'm ignoring the reasons for
scalar << 8 other than state that should be a conversion ctor.
As tested in main(),
A b;
A d = b;
is not an assignment - its a copy.
A c = 10;
is not an assignment - its a conversion.

class A
{
int nvalue;
public:
A() : nvalue(0) { } // def ctor
A(int scalar) : nvalue(scalar << 8) { } // conversion ctor
A(const A& copy) { nvalue = copy.nvalue; } // copy
A& operator=(const A& rhv) // assignment
{
if (this == &rhv) return *this;
nvalue = rhv.nvalue;
return *this;
}
};

int main()
{
A a; // def ctor, nvalue = 0
A b(1); // conversion, nvalue = 256
A c = 10; // conversion!!!, nvalue = 2560;
c = b; // assignment, c.nvalue = 256
A d = b; // copy!!!!, d.nvalue = 256
return 0;
}

Perhaps i'm barking up the wrong tree.

Oct 6 '06 #4
david wrote:
Hello,

I'd like to know whether the compiler can detect a member variable which is
not initialized (on purpose) by the constructor.

Easier with an simple example (the class is supposed to be a fixed point
variable):

class A
{
private:
int my_value;
A (int init) { my_value = init; }
public:
A () {}
A (const A& init) { my_value = init.my_value; }
A& operator = (int scalar) { my_value = scalar << 8; }
...
};

int main (void)
{
int p, q;
A x, y;

p = q;
y = x;
}

The compiler complains about q beeing used but not initialized,
is there a way for it to complain about x too ?

(yes I could put 0 in the constructor, but code size is critical in the
embedded environment I use, and I'm curious too :-)
Thomas answered your question, but let me suggest that neither you nor
almost anyone knows enough about the language, the particular
compiler/optimizer, and the particular target systems to correctly
guess what will generate more code at this fine-grained level. But this
is not a bad thing. High-level languages exist to allow you to focus on
the high-level program, not the low-level details of your machine. On
the other hand, optimizers are usually quite good at figuring out what
will produce the smallest code (or the fastest code, depending on your
compiler switches), and you should use them to that end until
measurement -- not programmer intuition! -- tells you that you should
hand-optimize (probably in assembly).

Several examples:

1. You should generally use initialization rather than assignment when
possible. Your code above calls the constructor for x and y and then
calls the assignment operator for y, whereas it really only needed to
call the constructor for x and the copy constructor for y if you were
to declare and initialize y at the same point:

A y = x; // Or A y(x);

Maybe the optimizer will inline or get rid of the extra call, but maybe
not.

2. Initialization lists generally give better code for similar reasons.
In your trivial example above, it may not matter, but it certainly does
matter in lots of real circumstances
(http://www.parashift.com/c++-faq-lit...html#faq-10.6).

3. You have private data in your class, which, according to the
standard, the compiler is free to move around and, much to your
chagrin, pad (sometimes that is fully or partially controllable by
compiler switches and/or #pragmas).

Anyway, the point is that you are probably prematurely optimizing (cf.
the section titled "Beware Premature Optimization" in
http://www.gotw.ca/publications/mill09.htm).

Cheers! --M

Oct 6 '06 #5

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

Similar topics

3
4570
by: Jun | last post by:
I have following script <script> var Animal = function(name){ this.name = name; } Animal.prototype.eat = function (food) {
15
21204
by: A | last post by:
Hi, A default copy constructor is created for you when you don't specify one yourself. In such case, the default copy constructor will simply do a bitwise copy for primitives (including pointers) and for objects types call their default constructor. Any others points i should know?
23
5186
by: Fabian Müller | last post by:
Hi all, my question is as follows: If have a class X and a class Y derived from X. Constructor of X is X(param1, param2) . Constructor of Y is Y(param1, ..., param4) .
18
3005
by: Matt | last post by:
I try to compare the default constructor in Java and C++. In C++, a default constructor has one of the two meansings 1) a constructor has ZERO parameter Student() { //etc... } 2) a constructor that all parameters have default values
9
2373
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a class, if the class has more than than one cosntructor, by making sure that each constructor has a different signature. I have managed to learn that and get
45
6369
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using parameters, I get CS1501 (no method with X arguments). Here's a simplified example which mimics the circumstances: namespace InheritError { // Random base class. public class A { protected int i;
8
4301
by: shuisheng | last post by:
Dear All, I am wondering how the default copy constructor of a derived class looks like. Does it look like class B : public A { B(const B& right) : A(right) {}
74
16040
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
13
2392
by: sam_cit | last post by:
Hi Everyone, I have the following unit to explain the problem that i have, class sample { public : sample() { printf("in sample...\n"); }
0
9699
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
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, 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...
0
10068
tracyyun
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...
1
7600
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
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();...
0
5625
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
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...

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.