Solved

nested classes

Posted on 1998-11-17
7
220 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
Independent Software Vendors: 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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