Solved

nested classes

Posted on 1998-11-17
7
215 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
  • 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  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 …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now