Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initialization of object

Reply
Thread Tools

Initialization of object

 
 
Rahul
Guest
Posts: n/a
 
      04-12-2008
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 ! ! !
 
Reply With Quote
 
 
 
 
joseph cook
Guest
Posts: n/a
 
      04-12-2008
> 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...

 
Reply With Quote
 
 
 
 
David Côme
Guest
Posts: n/a
 
      04-12-2008
On Sat, 12 Apr 2008 13:26:28 +0200, Rahul <(E-Mail Removed)> wrote:

> 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);
}
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-13-2008
On 12 avr, 13:26, Rahul <(E-Mail Removed)> wrote:

> 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:(E-Mail Removed)
Conseils en informatique oriente objet/
Beratung in objektorientierter Datenverarbeitung
9 place Smard, 78210 St.-Cyr-l'cole, France, +33 (0)1 30 23 00 34
 
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
what's the difference between value-initialization and default-initialization? Jess C++ 23 05-04-2007 03:03 AM
array initialization in initialization list. toton C++ 5 09-28-2006 05:13 PM
Initialization of non-integral type in initialization list anongroupaccount@googlemail.com C++ 6 12-11-2005 09:51 PM
Initialization via ctor vs. initialization via assignment Matthias Kaeppler C++ 2 07-18-2005 04:25 PM
Default Initialization Vs. Value Initialization JKop C++ 10 09-22-2004 07:26 PM



Advertisments