Copy Constructor from super class?

This is a discussion on Copy Constructor from super class? within the C++ Programming forums, part of the General Programming Boards category; Hi, i have a class CVImage and its subclass CVImageLabelator, I want to instantiate a CVImageLabelator from a CVImage but ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question Copy Constructor from super class?

    Hi,

    i have a class CVImage
    and its subclass CVImageLabelator,
    I want to instantiate a CVImageLabelator from a CVImage but I am getting errors and I think is because this new instance is not well initialized.
    So, what is the correct way to implement a copy constructor that takes as argument a super class object.
    I have this:

    Code:
    CVImageLabelator(CVImage image):CVImage(image)
    {
    Set(image.GetW(), image.GetH(), image.GetDepth(), image.GetChannel()); 
    };
    Where Set() is a method where I initialize private members that are needed to call CVImageLabelator public methods.
    Mac OS 10.6 Snow Leopard : Darwin

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    NEI. Post your error messages and relevant code snippets.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question

    sorry it was not sufficient...
    here is part of my CVImageLabelator implementation:
    Code:
    class CVImage;
    class CVImageLabelator : public CVImage
    {
    public:
    
    	CVImageLabelator():m_label(NULL) {};
    	CVImageLabelator(CVImage image):CVImage(image){Set(image.GetW(),image.GetH(),image.GetDepth(), image.GetChannel());};
    	~CVImageLabelator(){ delete [] m_label;};
    
    	void Set(const int w, const int h, const int depth, const int channels);
    	void Labeling();
    	int GetNumberOfLabels() const;
    
    	int At(const int x, const int y);
    	int At(const int x, const int y) const;
    	void SetAt(const int x, const int y,int value);
    	void SetAt(const int x, const int y,int value) const;
    	
    	//int& operator() (const int x, const int y) { return m_label[x+m_w*y]; };
    	//const int& operator() (const int x, const int y) const{ return m_label[x+m_w*y];};
    
    	CvPoint FingerTipPoint();
    
    private:
    	int *m_label;
    	int m_numlabels;
    };
    and this are the constructors of CVImage:
    Code:
    class CVImage
    {
    public:
    	CVImage():m_img(NULL) {}
    	CVImage(const int w, const int h, const int depth, const int channels):m_img(NULL) { Set(w, h, depth, channels); }
    	CVImage(const char* name, const int num = 1):m_img(NULL) { Load(name, num); }
    	CVImage(const CVImage& src):m_img(NULL) { Clone(src); }
    	~CVImage() { Release(); }
    ...
    }
    and when doing this:
    Code:
    	CVImageLabelator tmp = CVImageLabelator(m_img); //m_img is CVImage instance
    	CvPoint fingerTip = tmp.FingerTipPoint();
    I got runtime errors in the second line.
    I have tested the code with another structure and it runs fine, so the problem is not inside FingerTipPoint(), as I said before I am quite sure is a constructor problem.

    prove: I tried this at the beginning of FingerTipPoint():
    Code:
    std::cout<<this->At(GetW()-1,GetH()-1) << std::endl;
    and get :
    Code:
    Unhandled exception at 0x0040f788 in MultiMap.exe: 0xC0000005: Access violation reading location 0x066fdffc.
    I wonder what I am missing the constructor
    Mac OS 10.6 Snow Leopard : Darwin

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,676
    The run time message you are getting indicates you have performed an invalid operation on a pointer (eg dereferencing a NULL).

    Looking at the copy constructor for CVImage, it is initialising m_img to be NULL and then calling Clone(src). My guess is that the problem is in that clone function ... but you haven't shown that function.

    As a rough rule, you will get more effective help on forums by posting small and complete samples of code that illustrate your problem. You have paraphrased (i.e. quoted bits of code that you think are relevant, and left out others). Unless you know what the actual problem is, paraphrasing is a very effective method of leaving out necessary information relevant to your problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The error message is saying that you are accessing memory you shouldn't be (an uninitialized pointer, out-of-bounds access?). Your code doesn't really show why that might happen.

    The only thing fishy about the constructor is calling Set. Doesn't CVImage really not set those when it is copied? If so, won't this data be lost in the first place when you pass image by value to the constructor?

    Also, what if you replaced dynamically allocated arrays with std::vector?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    The only problem with your consutrctor is that you don't initialise m_numlabels
    If that doesn't fix it then your problem is elsewhere.

    Do you tell your mechanic that there's obviously a problem with your car's tyres because the engine wont start?
    Stop tell us that you know where the problem is and giving examples that leave out all the useful bits and only give us irrelevant and completely inconclusive answers. Some of us have been debugging this kind of thing for probably ten years longer than you have.
    Aparently your crash is inside a multimap. You haven't shown any code that contains a multimap!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    Question

    Hey, Thanks for your answers, they were correct in some way.
    The problem was my constructor should pass data by reference and not by value.
    I actually come from an Objective-C background where everything is done by reference so I didn't realize this.

    I just have a question.
    Passing an object by reference pass the pointer right? (So it does a swallow copy) and
    Passing by value copies it right?(So it does a deep copy)
    If this is correct why my later constructor was not working? I mean, is the object content is copied why all the members are not copied? As anon said:
    The only thing fishy about the constructor is calling Set. Doesn't CVImage really not set those when it is copied? If so, won't this data be lost in the first place when you pass image by value to the constructor?
    Why data is lost if its supposed to be a copy?

    I don't think I am understanding well this value and reference difference in C++
    I would appreciate a explanation about this.

    Thanks

    Ignacio
    Mac OS 10.6 Snow Leopard : Darwin

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,031
    Quote Originally Posted by nacho4d View Post
    Passing an object by reference pass the pointer right? (So it does a swallow copy) and
    Passing by value copies it right?(So it does a deep copy)
    No. A reference and a pointer is not the same thing. Beware. A reference is a reference. The standard doesn't say that it is anything else.
    A reference is not necessarily copied, but the object that it's a reference to isn't copied, so you could say it's a shallow copy. Same with pointers.
    You are correct about pass-by-value.

    If this is correct why my later constructor was not working? I mean, is the object content is copied why all the members are not copied? As anon said:

    Why data is lost if its supposed to be a copy?
    Data isn't lost when you copy, but you should beware of pointers. Destructors usually clean up raw pointers in a class, and if you have two copies of those, the copy might point to invalid data.
    A reference cannot be automatically copied.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linker error
    By tasha302 in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2006, 12:00 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 03:02 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21