Initialization of object

Discussion in 'C++' started by Rahul, Apr 12, 2008.

  1. Rahul

    Rahul Guest

    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 ! ! !
     
    Rahul, Apr 12, 2008
    #1
    1. Advertising

  2. Rahul

    joseph cook Guest

    > 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...
     
    joseph cook, Apr 12, 2008
    #2
    1. Advertising

  3. On Sat, 12 Apr 2008 13:26:28 +0200, Rahul <> 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);
    }
     
    David Côme, Apr 12, 2008
    #3
  4. Rahul

    James Kanze Guest

    On 12 avr, 13:26, Rahul <> 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:
    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
     
    James Kanze, Apr 13, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    10
    Views:
    960
  2. Matthias Kaeppler
    Replies:
    2
    Views:
    457
    Victor Bazarov
    Jul 18, 2005
  3. Replies:
    6
    Views:
    471
    Ron Natalie
    Dec 11, 2005
  4. toton
    Replies:
    5
    Views:
    943
    Victor Bazarov
    Sep 28, 2006
  5. Jess
    Replies:
    23
    Views:
    953
Loading...

Share This Page