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.
- 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