1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Pointer and Class

Discussion in 'C++' started by inspiration, Jun 1, 2010.

  1. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    Hey Guys,
    I've got the following problem:

    classA.hpp:
    Code:
    class  ClassA: public ClassB 
    ( 
    
    / / ... 
    ClassC C; 
    
    / / ... 
    
    )
    When calling the constructor of ClassA is yes, then the constructor of ClassB called.
    On the "return" is with me then seems implicitly to the constructor of ClassC opened in which the line "m_set = NULL;" a variable from ClassB überballert, missing me then in ClassA.


    Now I have the times changed so and the problem was gone :

    classA.hpp:
    Code:
    class  ClassA: public ClassB 
    ( 
    
    / / ... 
    ClassC * C; 
    
    / / ... 
    
    )
    classA.cpp:
    Code:
    ClassA:: ClassA (): ClassB () 
    ( 
     C = new ClassC (); 
     //.... 
    ) 
    
    ClassA:: ~ ClassA () 
    ( 
     delete C; 
    )
    And of course the calls to the member functions of C everywhere "->" instead of ".."

    My question is whether the now has a technical C + +-specific reason why this one works and the other not, or whether I merely by changing the actual memory usage error bedspread.
     
  2. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    I do not understand :(
     
  3. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    class ClassA: public ClassB 
    ( 
    
    / / ... 
    ClassC C; 
    
    / / ... 
    
    )
    What's going well?
    You create a static variable, once your ClassA is created in the constructor ... ClassC and behaves just as expected, it calls for exactly that generate the "default constructor" on .... And one instance of ClassC exists now cheerful and happy as a local variable in your constructor to the death ... aehm to your definition of the scope ends ... ie the constructor of ClassA to end is ... then ClassC is of course also partitioned Professionally, that is called the destructor deinner ClassC and destroyed the instance ....

    with

    Code:
    ClassC * C;
    verhinderst you only ... Now you put a variable of type "pointer to ClassC" (an internal address, usually 32 bit big), it points to something, but nothing usable. For the proper use of nu are themselves responsible ... ie if you create an instance of the ClassC and assigns C

    Code:
    C = new ClassC ()
    have the Zesto again somewhere ...
    But watch out, your hand C is of course only valid in the constructor ... So ClassC need in constructor of ClassA again destroy, or make the dynamically generated class using NEN ClassC pointer elsewhere known that you can destroy them anywhere else ...

    Principle: they are all viable ways it depends what you want to do it ... ned and describe in your post. So can you also tell keienr what is right and what is wrong ...
     
  4. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    See no essential difference between the two versions: both times is called the constructor of ClassC, and implicitly at the first, second, when explicitly.
     
  5. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Mist Ack, sorry, have not noticed the definition it is not the constructor of your class but ....

    It changes the following ...

    C is your member variable, so long as valid as the instance of A exists ...
    ClassC is created before the constructor of class A is called ...
    ClassC is destroyed, was executed after the desturktor of ClassA and before class A wid definitively destroyed ....
     
  6. meyup

    meyup New Member

    Joined:
    Feb 15, 2010
    Messages:
    102
    Likes Received:
    0
    Trophy Points:
    0
    hey techme, why should C be a local variable in the constructor?
     
  7. techme

    techme New Member

    Joined:
    Feb 15, 2010
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    because I have read nonsense :D
    looked for me like ClassA:: ClassA ClassA instead: public ClassB ...



    YOU should post more of the problemn ... because such sweeping statements about static vs mebers. Members can make dynamic ned ....

    overwriting, and that the variables "m_set = NULL;"
    How can the constructor of ClassB uberballern variable ClassC ne ""??
     
  8. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    Hi again,
    grad that's my problem.
    ClassC ClassA and ClassB has actually nothing to do, except that one instance of it in member variable ClassA.

    We, the constructor of ClassC implicitly called (first version), then I get an error later in the program, because I said was written in the memory variable destroyed (Debug led to the realization that it happened in the constructor of ClassC and m_set is a private variable of ClassC, has nothing to so with nothing really too ClassA or ClassB ).

    I choose the variant pointer and call the constructor via "new on" does not happen.

    Maybe it is just a compiler error or something. These memory errors are so often like searching for a needle in a haystack.

    Or it may be that the real mistake happened somewhere else entirely and I now only have the memory somewhat restructured, so it happened to no longer "pops"?

    I personally have always thought it was just as Croesus had said it:


    ... and I have not found anything else.

    But after this effect, I thought to myself, I ask for time with you, because maybe one of you ne other info. Man finally learns is never, eh .

    So first thank you for your answers
     
  9. creative

    creative New Member

    Joined:
    Feb 15, 2010
    Messages:
    87
    Likes Received:
    0
    Trophy Points:
    0
    The difference is that happens in the version "member variable" to the constructor of C between the constructor of ClassA and ClassB, the version with the pointer, it happens in the constructor of ClassA.

    ClassA:: ClassA (): ClassB (), C () () / / <-try the times
    is different from
    ClassA:: ClassA (): (C), ClassB () ()
     

Share This Page