![]() |
| | #1 |
| Registered User Join Date: Jun 2006
Posts: 114
| Defining derivated class problem Code: class class1; //Declaration
class class2
{
private:
class1 *ptr;
......
};
class class1 {...}; //Definition
Code: class base
{
public:
base() {}
~base() {}
virtual void uselessFunction() = 0; //Just to make it a base class :)
};
class class1 : public base; //Declaration... doesn't work :(
class class2 : public base
{
private:
class1 *ptr;
public:
void uselessFunction() {}
};
class class1 : public base
{
public:
void uselessFunction() {}
};
Thanks! |
| mikahell is offline | |
| | #2 |
| Registered User Join Date: Jan 2005
Posts: 7,250
| Code: class class1; |
| Daved is offline | |
| | #3 |
| Registered User Join Date: Jun 2006
Posts: 114
| Well... It doesn't work for me... Maybe I wasn't clear enough... Let's say in "class2", I want to use my "undefined class1 pointer" to call a method from the "base" class. If I only define class1 as "class class1;" before class2, then the compiler will complain on the following method Code: class2::someMethod(void)
{
ptr->uselessFunction(); //Remember, "ptr" is a pointer on "class1" here, not "class1:public base" :(
}
|
| mikahell is offline | |
| | #4 |
| Registered User Join Date: Jan 2005
Posts: 7,250
| This should have nothing to do with the base class. If you use the class1 pointer at all, you have to define it before you use it. add class2::someMethod after you define class1. Like this: Code: class base
{
public:
base() {}
virtual ~base() {}
virtual void uselessFunction() = 0; //Just to make it a base class :)
};
class class1;
class class2 : public base
{
private:
class1 *ptr;
public:
void someMethod();
void uselessFunction() {}
};
class class1 : public base
{
public:
void uselessFunction() {}
};
void class2::someMethod()
{
ptr->uselessFunction();
}
Last edited by Daved; 08-22-2007 at 03:44 PM. |
| Daved is offline | |
| | #5 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| To expand on what Daved says: The class1 forward declaration allows you to declare a pointer to an yet-unknown structure/class - but you can't use any of it's content until you've told the compiler what it is and where it is (and which class it inherits). For the compiler to be able to figure out what "uselessFunction()" is, and how it's called, it needs to know what class1 looks like. So, Daved's solution is the right one - declare both classes then declare any functions/methods. (Since I'm fairly new to C++, I actually took your code and tried a similar thing to what Daved suggested before posting a reply here, Daved probably KNEW that already!) -- Mats |
| matsp is offline | |
| | #6 |
| Registered User Join Date: Jun 2006
Posts: 114
| Defining both classes methods afterwards.... Oops, forgot to try this solution :P Logically this should solve my problem, thanks to you both! |
| mikahell is offline | |
| | #7 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats | |
| matsp is offline | |
| | #8 |
| Registered User Join Date: Jan 2005
Posts: 7,250
| >> that's the ONLY consequence of having the declaration there. Putting the function definition inside the class definition also has the effect of marking the function as inline. While many optimizing compilers make their own decisions about what to inline, they do take into consideration the programmers suggestions. Many people like to have some one line functions (like get and set methods) declared inline in the class definition. However, if you're going to be using a variable of a class that you forward declared, it is best to just make the function definition outside of the class definition. Also note that normally these things are broken up in header and source files. For example: Code: // base.h
#ifndef BASE_H
#define BASE_H
class base
{
public:
base() {}
virtual ~base() {}
virtual void uselessFunction() = 0; //Just to make it a base class :)
};
#endif
Code: // class2.h
#ifndef CLASS2_H
#define CLASS2_H
#include "base.h"
class class1;
class class2 : public base
{
private:
class1 *ptr;
public:
void someMethod();
void uselessFunction() {}
};
#endif
Code: // class1.h
#ifndef CLASS1_H
#define CLASS1_H
#include "base.h"
class class1 : public base
{
public:
void uselessFunction() {}
};
#endif
Code: // class2.cpp
#include "class2.h"
void class2::someMethod()
{
ptr->uselessFunction();
}
Last edited by Daved; 08-22-2007 at 03:44 PM. |
| Daved is offline | |
| | #9 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
-- Mats | |
| matsp is offline | |
| | #10 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,271
| Also, don't forget to make your base class destructor virtual! Code: // base.h
#ifndef BASE_H
#define BASE_H
class base
{
public:
base() {}
virtual ~base() {}
virtual void uselessFunction() = 0; //Just to make it a base class :)
};
#endif
|
| cpjust is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Class Membership Problem | josephjah | C++ Programming | 5 | 05-27-2007 01:48 PM |
| Inheritance using Stack Class Problem | dld333 | C++ Programming | 17 | 12-06-2005 11:14 PM |
| Polymorphism Theory Question - Polymorphic Class Definition. | ventolin | C++ Programming | 3 | 10-31-2005 12:05 PM |
| static class problem. | Sebastiani | C++ Programming | 3 | 10-16-2002 03:27 PM |
| Difficulty superclassing EDIT window class | cDir | Windows Programming | 7 | 02-21-2002 05:06 PM |