silly little question on variable scope

Discussion in 'C++' started by Bit byte, Mar 23, 2006.

  1. Bit byte

    Bit byte Guest

    What's wrong with this:

    //file1.h

    Class A {
    public:
    A(){i=1;}
    ~A(){;}
    void doThis(void){;}
    void doThat(void){;}

    private:
    int i;
    };


    //file2.h
    #include "file1.h"

    A a;

    Class B {
    public:
    B(){
    //a = new A(); //will not compile
    //a.doThis();
    }
    ~B(){;}
    };


    Anyone know why I can't use a (type A) in class B ?
    Bit byte, Mar 23, 2006
    #1
    1. Advertising

  2. Bit byte

    Axter Guest

    Because you declared A as a concrete type instead of as a pointer.
    You need a pointer to use new A()

    A *a = new A;

    But in general, you should avoid using pointers if you don't have to.
    If you don't need to use the new operator to create an instance of an
    object, than don't use it, and declare it as a concrete object instead.
    Axter, Mar 23, 2006
    #2
    1. Advertising

  3. Bit byte

    Puppet_Sock Guest

    Bit byte wrote:
    > What's wrong with this:
    >
    > //file1.h
    >
    > Class A {
    > public:
    > A(){i=1;}
    > ~A(){;}
    > void doThis(void){;}
    > void doThat(void){;}
    >
    > private:
    > int i;
    > };
    >
    >
    > //file2.h
    > #include "file1.h"
    >
    > A a;
    >
    > Class B {
    > public:
    > B(){
    > //a = new A(); //will not compile
    > //a.doThis();
    > }
    > ~B(){;}
    > };
    >
    >
    > Anyone know why I can't use a (type A) in class B ?


    You can. Your problems don't arise from that.

    First, you've got a variable at file scope in an h file.
    You don't usually want that. If you include file1.h in
    more than one .cpp file, you get trouble. If you want
    an instance of class A at file scope, you need to
    declare it in the .cpp file where you want it, not in
    a header file.

    Next, as somebody pointed out, you've got a call
    to new and you are putting the result into a non-pointer.
    Calls to new return pointers to allocated memory.
    Read up on pointers.

    Next, you are doing something pretty whack even
    if you do change the variable a to a pointer, and
    even if you do have only one instance of a at
    file scope. You are assigning a pointer to a "newed"
    instance to the variable a. But the variable a is
    outside the scope of the class B that is doing it.
    So, the second time you do this, you will wind
    up with a resource leak.

    Probably what you want is something like so. First,
    drop the variable a entirely. Then add a pointer as
    a member of class B. Then add the appropriate
    thing to the dtor of B to avoid the memory leak.

    #include "file1.h"

    Class B {
    public:
    B(){
    pa = new A(); //will not compile
    pa->doThis();
    }
    ~B(){delete pa;}
    private:
    A * pa;
    };

    Socks
    Puppet_Sock, Mar 23, 2006
    #3
    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. Paul Opal
    Replies:
    12
    Views:
    934
    Paul Opal
    Oct 11, 2004
  2. ThaDoctor
    Replies:
    3
    Views:
    374
    Alan Woodland
    Sep 28, 2007
  3. David Filmer
    Replies:
    19
    Views:
    228
    Kevin Collins
    May 21, 2004
  4. Andrew Falanga
    Replies:
    2
    Views:
    194
    Andrew Falanga
    Nov 22, 2008
  5. Daniel
    Replies:
    1
    Views:
    203
    Bart van Ingen Schenau
    Jul 9, 2013
Loading...

Share This Page