Basic C++ OOP - Constructors
I have a dumb question about constructors. As I was beginning to ask another question I think I just figured out what I did wrong.
If I have a constructor with arguments, is the constructor called when I create the object, or when I pass arguments to it? for example, I have a class called PixelCount which has an argument in the constructor. Code:
PixelCount x; //here? |
Quote:
Quote:
The second line does not call any constructor nor pass an argument to one. It calls the object's operator() function and is a compile time error if that is not defined. I think the code you intended is: Code:
PixelCount x(argv[1]); |
Alright here is my problem. I am new to Programming and C++, very new to classes and object oriented programming. Here is the include and source for a small class I'm working on.
Code:
#include <opencv/cv.h> Code:
#include "include/PixelCount.h" Code:
PixelCount x(argv[1]); |
Quote:
Here it is: Code:
#include <opencv/cv.h> Code:
g++ `pkg-config opencv --cflags` PixelCount.cpp `pkg-config opencv --libs` Code:
./a.out image.jpg |
Quote:
Quote:
My original code, which did worked looked quite similar to yours. The problems arose when I attempted to separate the class/include. It turns out that the problem was that I was not preceeding function names with "<classname>::" in the source files. Something I didn't have to do in the past when it was all in one file. Anyway, I appreciate you help. Just not the attitude. It's easy to look up information if you already know what you are doing? and not everyone who posts is just looking for quick answers. |
> > You have not fully indicated what is it that you did, and what results you are seeing.
> If that's the case you did an excellent job extrapolating! Yes he did, but next time you're asking for help don't rely on that: quote the actual error-message you got from the compiler/linker instead, and paste the relevant pieces of source. |
Keep this in mind: a "constructor" is just an initialization subroutine. That's it, that's all.
When C++ is told to instantiate a new object (and remember, it must be explicitly told to do so...), it basically does two things in this order:
A destructor, of course, is the opposite: "I'm getting ready to destroy this storage-block. If it refers to anything else that needs to be terminated or destroyed, please do so now." |
Quote:
The OS sets memory to zero before giving memory to the process (excepting cases where the memory is given with predefined contents, such as a file mapping). In a simple program, the memory used within the process, (heap, stack, static or global) for a new object is typically memory the run time library has received zero filled from the OS, so it is typically zero before the constructor is executed, suppressing the symptoms of many common bugs in classes that don't initialize everything they should in their constructors. But in more complex programs, memory for a new object (heap or stack) is more typically memory released from some earlier use within the same process, not zero filled memory newly received from the OS, so those bugs start to have symptoms. |
Quote:
|
All times are GMT -5. The time now is 09:46 AM. |