strange class declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • babu198649
    New Member
    • Sep 2007
    • 22

    strange class declaration

    hi
    i came across this following code could some one explain whats happening with the int refCount(ie.how can an argument be passed to an int) in constructor.

    Code:
    class SharedImage
    {
    public:    
        SharedImage() : refCount(0){};
        int refCount;
    };
  • tomPee
    New Member
    • Jun 2007
    • 21

    #2
    Originally posted by babu198649
    hi
    i came across this following code could some one explain whats happening with the int refCount(ie.how can an argument be passed to an int) in constructor.

    Code:
    class SharedImage
    {
    public:    
        SharedImage() : refCount(0){};
        int refCount;
    };
    Hi,

    SharedImage() is the constructor of the class, and it can intialize values ( even so before starting the body of the constructor, which is between the {}.

    This code simply states that the constructor of sharedImage initializes the datamember 'refCount' at 0.

    Initializating is needed when you:
    - Have a const data member in your class
    - Have a reference datamember in your class.
    - Inherit some class that doesn't have a default constructor (= constructor that doesn't need any parameters ).

    Putting:
    SharedImage(){
    refCount = 0;
    }

    Is not the same as initialization. As initialization is done before the constructor body even starts. The 3 thingies mentioned above have to be initialized.

    -Tom

    Comment

    • babu198649
      New Member
      • Sep 2007
      • 22

      #3
      thanks......... ............... :)

      Comment

      Working...