Click here to Skip to main content
15,917,618 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given the class

C++
class Subclass1;
class subclass2;

class MyClass
{
 Subclass1 m_subclass1;
 Subclass2 m_subclass2;
}

Please what is the format for writing an operator such that the cast:
C++
MyClasss obj;
Subclass1 *obj1 = (Subclass1 *)obj;
Subclass2 *obj2 = (Subclass2 *)obj;


Will succeed, returning the pointers to the sub objects.
All I need is the format of the prototype, I can continue from there.If I wish I can make the operator return a copy or reference rather than a pointer if I so desire.
Posted
Updated 9-Jun-14 22:32pm
v2
Comments
Legor 10-Jun-14 4:36am    
Considering only the code you've provided, subClass and MyClass are not related in a parent-child-relationship. So Subclass isn't a subclass of MyClass. Therefore they are complete different types and you cannot cast different types to another as stated in the Answer by Richard MacCutchan.

The Question is should SubClass1 and SubClass2 be childs of MyClass and you just failed to show the code?
Gbenbam 10-Jun-14 5:12am    
I apologise for the poor phrasing of the qestion.In spite of the poor phrasing someone succeeded in giving me the right answer as seen in solution two below.

You cannot cast different types to one another without breaking things. See http://msdn.microsoft.com/en-us/library/cscsdfbt(v=vs.71).aspx[^].
 
Share this answer
 
Comments
Gbenbam 10-Jun-14 5:20am    
I apologise for the poor phrasing of qestion.Inspite of that someone has given me the right solution as shown in solution2 below.
C++
class A
{
};

class B
{
};

class C
{
    A a;
    B b;
public:
    operator A* (){return &a;}
    operator B* (){return &b;}
};

int main()
{
    C c;
    A * pa = (A*) c;
    B * pb = (B*) c;
}
 
Share this answer
 
Comments
Gbenbam 10-Jun-14 5:07am    
Believe me.You deserve a big hug.Thank you.You just made my day.
Actually, I saw these pissibility while writing data base access program for access to SQL server database using ADO. I noticed that the _variant class had operators that could return copies of other objects.I saw the syntax of the operator.Its just like what you just shared with me now.Since all I have ever written before are mathematical operators like assignmrnt a.x equality etc, I felt I should get help.Once again, thanks a lot.
CPallini 10-Jun-14 10:59am    
You are welcome.
Gbenbam 10-Jun-14 18:23pm    
I believe you will find my reply to solution3 encouraging.
Joren Heit 10-Jun-14 9:53am    
Of course this will work, but why do you need a cast? Why not just have a function with a descriptive name that returns pointer (or even better, a reference) to the members you need?

EDIT: I posted an alternative solution below.
As mentioned in a reply to Solution 2, I doubt that it's necessary to phrase this feature as a casting operation. Why? Because you're not really casting the object to something else. You simply need access to one of its internals, which is naturally achieved through plain old member functions.

How I would do it:

C++
class A {};
class B {};

class C
{
    A a_obj;
    B b_obj;

public:    
    A const &getA() const;
    A &getA();

    B const &getB() const;
    B &getB();
};

int main()
{
    C c1;
    C const c2;

    A &a1 = c1.getA();
    A &a2 = c2.getA(); // Compiler error, assigning const& to non-const
    A const &a3 =  c2.getA(); // OK!
    // same for B
}


Notice that the integrity of the object is ensured by having the const members return const references.
 
Share this answer
 
v3
Comments
Gbenbam 10-Jun-14 17:53pm    
Thanks for your solutions.They are quite revealimg.I never new references could be declared like pointers until now.I declare references only in parameters.
As for my question.Solution2 is what I really need.Gets and Sets are,to me, an normal components of classes.They are ever present members of classes I write.
I need an operator instead of a get function because I wish to integrate an olf app to a new app
Having written a casting operator,all I need do is to cast the new object to thr approriate class; member data for the old app and lives goes on with it as usual.It is not aware that it has become a component of another app.

Once again, thanks for your solution.I leant very important facts about references from it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900