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

Initialization of object

Hi Everyone,

I have the following code,

class A
{
public : A()
{
}
void show()
{
}
};
int main()
{
cout<<"Enter the number : ";
int n;
cin>>n;
if(n<10)
goto label;
A obj;
label:
obj.show();
return(0);
}

and i get the expected error saying "initialization of obj is skipped
by goto label". But when i remove the definition of the constructor
from the class, i don't the error, why is this so?

Thanks in advance ! ! !
Jun 27 '08 #1
3 1233
On Sat, 12 Apr 2008 13:26:28 +0200, Rahul <sa*****@yahoo.co.inwrote:
Hi Everyone,

I have the following code,

class A
{
public : A()
{
}
void show()
{
}
};
int main()
{
cout<<"Enter the number : ";
int n;
cin>>n;
if(n<10)
goto label;
A obj;
label:
obj.show();
return(0);
}

and i get the expected error saying "initialization of obj is skipped
by goto label". But when i remove the definition of the constructor
from the class, i don't the error, why is this so?

Thanks in advance ! ! !
NEVER USE GOTO !!
Do it :
int main()
{
cout<<"Enter the number : ";
int n;
cin>>n;
if(n<10)
{
A obj;
obj.show();
}
return(0);
}
Jun 27 '08 #2
and i get the expected error saying "initialization of obj is skipped
by goto label". But when i remove the definition of the constructor
from the class, i don't the error, why is this so?

Thanks in advance ! ! !
"A program that jumps from a point where a local variable
with automatic storage duration is not in scope to a
point where it is in scope is illformed unless the variable
has POD type (3.9) and is declared without an initializer
(8.5)."

A more interesting question might be why if you changed your
definition of show() to:
void show
{
std::cout<<"I got called!"<<std::endl;
}

You would see no output (no matter the user input). That doesn't
seem right to me...

Jun 27 '08 #3
On 12 avr, 13:26, Rahul <sam_...@yahoo.co.inwrote:
I have the following code,
class A
{
public : A()
{
}
void show()
{
}
};
int main()
{
cout<<"Enter the number : ";
int n;
cin>>n;
if(n<10)
goto label;
A obj;
label:
obj.show();
return(0);
}
and i get the expected error saying "initialization of obj is
skipped by goto label". But when i remove the definition of
the constructor from the class, i don't the error, why is this
so?
Because you're declaring an uninitialized object, and
presumably, your code is capable of handling uninitialized
objects. (In this regard, an object is "initialized" if there
is explicit initialization, or the type of the object has a
non-trivial constructor.)

In real code (which doesn't use goto), this is still relevant in
switch statements, whose semantics is exactly that of a goto.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #4

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
17
by: Thomas Lorenz | last post by:
Hello, I'm a little confused about object initialization. According to Stroustrup (The C++ Programming Language, Special Edition, Section 10.2.3) constructor arguments should be supplied in one...
4
by: Brian Folke Seaberg | last post by:
I have been examining the documentation for a class library written in C++. It seems that most of the classes in that library use two stage initialization. Objects are not ready for use after...
5
by: BigMan | last post by:
Does the standard define the order in which static members of a class are created and initialized?
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
10
by: utab | last post by:
Dear all, Can somebody direct me to some resources on the subject or explain the details in brief? I checked the FAQ but could not find or maybe missed. Regards,
1
by: Sandro Bosio | last post by:
Hello everybody, my first message on this forum. I tried to solve my issue by reading other similar posts, but I didn't succeed. And forgive me if this mail is so long. I'm trying to achieve the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
4
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...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
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?
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
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...
0
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...

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.