Solved

nested classes

Posted on 1998-11-17
7
219 Views
Last Modified: 2012-05-04
how do i access the member function of a class inside another class? the compiler keeps telling me " pointer to structure required on the leftside of -> or ->* "

the declaration of the classes is shown below.

class Point
{
public:
void A();
};

void Point::A()
{
// do nothing
}
class Plane
{
public:
Point tx;
};

void main()
{
Plane *myplane = new Plane;

  myplane->tx->A();//complier reported error on this line.



 
0
Comment
Question by:deuter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 

Author Comment

by:deuter
ID: 1178111
Edited text of question
0
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 1178112
Simply use
myplane->tx.A();
As 'tx' is already an instance of 'Point' (and no pointer to 'Point'), you don't have to dereference it...
0
 

Author Comment

by:deuter
ID: 1178113
thanks, it saves me from redesigning my program but if you dont mind , can you explain is there any difference if i declare my class Point inside the Plane class? is it legal? if yes, is there any difference from the above declaration?

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:jkr
ID: 1178114
It is legal, but gets inconvenient to handle, as the declaration is only valid inside the class (the scope is restricted to it, the declaration is 'local' to the class) - i think it's better to keep your current method...
0
 
LVL 22

Expert Comment

by:nietod
ID: 1178115
>> It is legal, but gets inconvenient to handle
I think he means its inconvenient in this case.  because you will probably want to access the point class from outside fo the place class.  There are plenty of cases where nested classes make a lot of sense and are not inconvenient.  This just isn't one of them.
0
 
LVL 86

Expert Comment

by:jkr
ID: 1178116
nietod - that's exactly what i meant...
deuter - consider the following:
class __DYNLINK CDaoTableDir:   public CObject
{

    class CDaoTableDirEnt
    {
    public:

                            CDaoTableDirEnt ();
                            CDaoTableDirEnt (   CDaoDatabase*   pdaoDB,
                                                CDaoTableDef*   ptd     =   NULL,
                                                CDaoRecordset*  prs     =   NULL
                                            );
        // etc...
    };

public:
    // etc...
};

In this example, the constructor of the nested class is

CDaoTableDir::CDaoTableDirEnt::CDaoTableDirEnt  (   CDaoDatabase*   pdaoDB,
                                                    CDaoTableDef*   ptd,
                                                    CDaoRecordset*  prs
                                                )
{
    m_pdaoTD    =   ptd;
    m_pdaoSet   =   prs;

    Create  (   pdaoDB);
};

and will _only_ be accessible (AND visible) from within instances of 'CDaoTableDir'
0
 

Author Comment

by:deuter
ID: 1178117
jkr, thanks for the info, i 've learnt a lot!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question