Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
81 user(s) are online (49 user(s) are browsing Forums)

Members: 1
Guests: 80

geennaam, more...

Headlines

 
  Register To Post  

I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
I'm trying to do a forward declaration of a simple class.


Class MyA;
Class 
MyB;

Class 
MyA()
{
    
MyB ToMyB()
    {
        
MyB temp;
        return 
temp;
    }
}

Class 
MyB()
{
    
MyA ToMyA()
    {
        
MyA temp;
        return 
temp;
    }
}


Just like that, any ideas how its possible to do that?
I get a error saying MyB is incomplete type.


(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Quite a regular
Quite a regular


See User information
@LiveForIt

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).

Back to a quiet home... At last
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
@abalaban

I go whit pointers to the Classes instead, it looks like it work, thanks.

(NutsAboutAmiga)

Basilisk II for AmigaOS4
AmigaInputAnywhere
Excalibur
and other tools and apps.
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Quite a regular
Quite a regular


See User information

Retired
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
@LiveForIt

Your example case requires infinite memory! MyA contains a MyB which contains MyA ad infinitum, so it's just as well it'll never compile

Go to top
Re: I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
@broadblues


No it should not take infinity memory.

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.
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
@broadblues

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.
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
@LiveForIt

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.
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Quite a regular
Quite a regular


See User information
@LiveForIt

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.

Back to a quiet home... At last
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Home away from home
Home away from home


See User information
@abalaban

Quote:
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.
Go to top
Re: I'm trying to do a forward declaration of a simple class.
Quite a regular
Quite a regular


See User information
@LiveForIt

Quote:
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.


Yep after re-reading again you are right I thought they were attribute members.

Quote:
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.


I don't see how the "copy constructor" has anything to do with new/delete. Consider the following use case

SomeClass piMyVar1 = new SomeClass();
if(
NULL != piMyVar1)
{
    
piMyVar1->SetParameter1(someValue);

    
SomeClass iMyVar2(*piMyVar1); // copy constructor

    
delete piMyVar1piMyVar1 NULL// deletion

    
iMyVar2.GetParameter1(); // still able to access iMyVar2

    // ... some work
// when exiting this block iMyVar2 becomes out of scope and cannot be referred to anymore

Back to a quiet home... At last
Go to top

  Register To Post

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project