Sandra-24
asked on
Equiv of function prototype for classes?
I got myself into a chicken and egg scenario.
With functions you can do the following:
void a();
void b()
{
a();
}
void a()
{
cout << "Hello World";
}
But with classes what do you do?
I have two classes, A, and B. B inherits from A. A creates an instance of B in one of it's member functions.
Obviously A has no idea that B even exists and If I were to move B above A in the source file then B will complain that A doesn't exist!
What do I do? Is it possible to create an object of B inside of A? I would think it is because you could create an A object inside of A.
Thanks,
-Sandra
With functions you can do the following:
void a();
void b()
{
a();
}
void a()
{
cout << "Hello World";
}
But with classes what do you do?
I have two classes, A, and B. B inherits from A. A creates an instance of B in one of it's member functions.
Obviously A has no idea that B even exists and If I were to move B above A in the source file then B will complain that A doesn't exist!
What do I do? Is it possible to create an object of B inside of A? I would think it is because you could create an A object inside of A.
Thanks,
-Sandra
This will also work in several source files:
// In A.h
class B; // Forward declaration
class A
{
public:
B b;
};
// In B.h
#include "A.h"
class B : public A
{
};
// In A.h
class B; // Forward declaration
class A
{
public:
B b;
};
// In B.h
#include "A.h"
class B : public A
{
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
For mnashadka
class B; // Forward declaration
class A
{
public:
B b;
};
That WON'T be even compiled because compiler don't know the size of class B to generate "B b;" and forward declaration can't help
But using pointers can help
////////////////
// A.h
class B;
class A
{
public:
B* b;
};
////////////
// B.h
#include "A.h"
class B : public A
{
};
For Sandra-24
What's problem you want to solve?
class B; // Forward declaration
class A
{
public:
B b;
};
That WON'T be even compiled because compiler don't know the size of class B to generate "B b;" and forward declaration can't help
But using pointers can help
////////////////
// A.h
class B;
class A
{
public:
B* b;
};
////////////
// B.h
#include "A.h"
class B : public A
{
};
For Sandra-24
What's problem you want to solve?
ASKER
The problem is a little lengthy:
Time object has a aToString() method that will create a string object with the time according to format rules you give it. (like PHP's Date function: http://ca3.php.net/manual/en/function.date.php )
Now LocalTime inherits everyhting from Time, and adds the ability to work with timezones. Instead of copy and pasting the ToString function to LocalTime and make it virtual (which means code duplication) I got around that by providing virtual methods for working with timzones that return error values in Time objects but function as expected on LocalTime objects. This enabled me to put the formatting rules for timezones into the ToString() function of Time. If the object is a Time function it will simply ignore the formatting options for timezones. And everything works as expected for LocalTime objects. But for one small issue. In a LocalTime object where the timezone is PST (-8) for example and you wish to convert it to Swatch time format, which is CET (+1) you need to convert the time to the new timezone. An elegant way of handling this is using LocalTime's functionality. A Copy constructor with two args will copy an existing LocalTime object to a new LocalTime object of a new timezone automatically converting the time if required. So LocalTime temp(*this,CET); will convert any time in any timezone to CET and then I can call the method on this new object to give me the correct Swatch time.
So now I'll just use new and delete with a LocalTime pointer, thanks!
-Sandra
Time object has a aToString() method that will create a string object with the time according to format rules you give it. (like PHP's Date function: http://ca3.php.net/manual/en/function.date.php )
Now LocalTime inherits everyhting from Time, and adds the ability to work with timezones. Instead of copy and pasting the ToString function to LocalTime and make it virtual (which means code duplication) I got around that by providing virtual methods for working with timzones that return error values in Time objects but function as expected on LocalTime objects. This enabled me to put the formatting rules for timezones into the ToString() function of Time. If the object is a Time function it will simply ignore the formatting options for timezones. And everything works as expected for LocalTime objects. But for one small issue. In a LocalTime object where the timezone is PST (-8) for example and you wish to convert it to Swatch time format, which is CET (+1) you need to convert the time to the new timezone. An elegant way of handling this is using LocalTime's functionality. A Copy constructor with two args will copy an existing LocalTime object to a new LocalTime object of a new timezone automatically converting the time if required. So LocalTime temp(*this,CET); will convert any time in any timezone to CET and then I can call the method on this new object to give me the correct Swatch time.
So now I'll just use new and delete with a LocalTime pointer, thanks!
-Sandra
class B; // Forward declaration
class A
{
public:
B b;
};
class B : public A
{
};