Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pointer to member functions

Status
Not open for further replies.

MvanH

Technical User
May 15, 2001
19
0
0
NL
Goodday people,

I'm trying out some pointer functions in Visual C++, to understand more
about C++
I have the following code in one of my class member functions (see bottom of post).

What I tried to do is allow the class this function is in to write into the
document that is attached to my CDocument class called CMainDoc.

However when I use this function I receive an error genereted (according to the debug window by following code)

_AFXCOLL_INLINE void*& CPtrList::GetHead()
--> { ASSERT(m_pNodeHead != NULL);
return m_pNodeHead->data; }

This means that m_pNodeHead = 0 ??

Does anybody know what is wrong with my code?

Thanks Martini.

memberfunction blabla::writeinto(CString datastring)
{
///............. rest of function
typedef void(CMainDoc::*pMaindoc) (CString);

pMaindoc tptr;
tptr = &CMainDoc::WriteInto;

CMainDoc p;
// CMainDoc *pt = &p;

(p.*tptr) ("Test\n");
// (pt->*tptr) ("Test2\n");
return erno;
}

 
Here is a sample class I wrote a while back so I could remember how to use method pointers. As you can guess, I am posting this because I dont remember myself how I did it.

Hope this helps
Matt


class c1{
public:
c1::c1(){}
void sample1();
void sample2();
void sample3();
void sample4();
void sample5();
void sample6();

};

void c1::sample1(){cout<<&quot;sample1\n&quot;;}
void c1::sample2(){cout<<&quot;sample2\n&quot;;}
void c1::sample3(){cout<<&quot;sample3\n&quot;;}
void c1::sample4(){cout<<&quot;sample4\n&quot;;}
void c1::sample5(){cout<<&quot;sample5\n&quot;;}
void c1::sample6(){cout<<&quot;sample6\n&quot;;}

class c2{
public:
c2::c2( void(c1::*ptr)() ):method_ptr(ptr){};
void setmethod(void(c1::*ptr)());
void fire();
private:

void (c1::*method_ptr)();
};


void c2::fire(){c1 temp;(temp.*method_ptr)();}
void c2::setmethod(void(c1::*ptr)()){method_ptr=ptr;}


int main()
{
c2 mc2(&c1::sample1);

mc2.fire();
mc2.setmethod(&c1::sample2);
mc2.fire();
mc2.setmethod(&c1::sample3);
mc2.fire();
mc2.setmethod(&c1::sample4);
mc2.fire();
mc2.setmethod(&c1::sample5);
mc2.fire();
mc2.setmethod(&c1::sample6);
mc2.fire();
}

 
Ok I think I know what the problem is.

When I call CMaindoc p;
A new document is created, as CMainDoc is derived from class CDocument.

the code above does not generate the error, but the fact that I try to write into a new documenten that is not yet initiated with OnNewDocument that is the problem.

Ok.. This finding just makes my problem bigger.

I still want to write into my document.
Preferebly by the use of my CMainDoc::WriteInto function.

The question now is, HOW do I call this function from another class?

Please help me...

Martini


 
in your constructor for CMainDoc add this line:

CDocument::OnNewDocument();// add appropriate params

OR

OnNewDocument(); // if you over-rode the inherited function


On a side note, you CAN use the second one and it will work fine BUT if you do inherit and do NOT call up to the parent class, the parent class may/may not like it so keep the
CDocument::OnNewDocument() in the back of your mind if you run into problems.

matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top