Solved

nested classes

Posted on 1998-11-17
7
216 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

920 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

16 Experts available now in Live!

Get 1:1 Help Now