You can't do that. Forward declaration of class (in other word incomplete type declaration) is only allowed when you are using pointer to that class. If you are declaring or using an instance then the compiler needs the full declaration (because it needs to know the size of the memory needed for that variable)...
If you need to cross reference the object (which is tiedous at destruction time) then you need to split the classes in separate files (don't forget the #ifndef CLASS_X at the top of each file).
Remember the classes are created when they are called. Normally you create a method that return the same class.
Class MyA()
{
public:
MyA()
{
printf(“created\n”);
}
~MyA()
{
printf(“deleted\n”);
}
MyA Magic()
{
MyA temp;
return temp;
}
}
int main()
{
MyA test;
test.Magic();
}
this is small example, what will happen is test is created, when main() is called. Next when method Magic is called, MyA Temp is created, value is returned, and temp is deleted, and when main() ends, test is automatically deleted..
created objects are handled by the language, how ever if you do a New MyA() its not deleted (at least that's what I believe).
I the case where MyB was private or public object whit in MyA, and MyA where private or public object whit MyB, then you be right, but in this case its created only whit in the methods.
(NutsAboutAmiga)
Basilisk II for AmigaOS4 AmigaInputAnywhere Excalibur and other tools and apps.
Class'es are basically none existent definition of what the object is going to be like, unless you define some thing as “static” whit in the class it self.
If you do static you can do
MyA::SomeThing()
But if SomeThing() is not static its not allowed, to call it from the Class, you first will need to creat a object.
Edited by LiveForIt on 2014/2/11 10:36:21
(NutsAboutAmiga)
Basilisk II for AmigaOS4 AmigaInputAnywhere Excalibur and other tools and apps.
As I'm learning more about how a class works, I found out this code might fail, and it might slow down the program.
MyB ToMyB()
{
MyB temp;
return temp;
}
C++ is evil it does a memory copy of the return value before returning by default, and then free the object, (Instead of creating a object and lett operator= handel it. )
The result is that if you have pointers declared as private or public inside object, they can end up being freed two times. (One time in the copy and one time real object.)
MyB &ToMyB()
{
return temp;
}
So by changing it to a by Reference is correct way to fix it. It is possible to use by pointer, but it uglifies the code.
(NutsAboutAmiga)
Basilisk II for AmigaOS4 AmigaInputAnywhere Excalibur and other tools and apps.
Ok you are learning C++. Try not to make assumption on what you are reading, you may make false one.
For example what Broadblues is telling is true about the cross inclusion of a plain instance (or a reference) of both classes in your first example. If Class A includes an instance of Class B which itself includes an instance of Class A then you have an infinite recursion requiring infinite memory storage just to start instantiating a single object.
About the copy it is exactly how C works when passing in parameters to functions or returning a value : it is copied on the stack, or in registers depending on the ABI, but anyway it is copied, if you don't want a copy then pass a pointer (or better in C++ pass a reference).
I am not sure what you want to say when you write "C++ is evil it does a memory copy of the return value before returning by default, and then free the object, (Instead of creating a object and lett operator= handel it. )" but first make sure that you understand what a "copy constructor" is, how it works and how it differs from the assignment operator.
That is true that this value copying can take time especially for huge objects that is exactly why reference exists, and combined with the 'const' modifier it prevents you from unwanted modification.
Note that returning a reference to a volatile variable is not the solution either unless you first make a deep copy of your object.
I am not sure what you want to say when you write "C++ is evil it does a memory copy of the return value before returning by default, and then free the object, (Instead of creating a object and lett operator= handel it. )" but first make sure that you understand what a "copy constructor" is, how it works and how it differs from the assignment operator.
So there is "copy constructor", that might solve some tricky cases.
Well what you see is that if you count number of “New” called, and compare it whit number of “Delete” calls, they do not match up.
I was suppressed by this unexpected behavior, that when I know some thing fishy was going on.
If it was handled whit the “new” and “operator=” then I most like never have run into this problem.
Quote:
instance of Class A then you have an infinite recursion requiring infinite memory storage just to start instantiating a single object.
Only if its declared it as public or private object, the return value and parameters are created / copied when you call the Method.
You don't need to inline, methods like I did, and that's probably how its possible to create objects, in any methods.
I find Classes are tricky devils, but well worth it when it finally working.
(NutsAboutAmiga)
Basilisk II for AmigaOS4 AmigaInputAnywhere Excalibur and other tools and apps.