C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

What's different? (empty interface inheritance)

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
kanam
Guest





PostPosted: Wed Aug 18, 2004 12:13 pm    Post subject: What's different? (empty interface inheritance) Reply with quote



i want to use class member function pointer.
i did as below case. but this program terminates with runtime error.

interface ICtlBase
{
virtual ~ICtlBase(){};
virtual void SetName(CString strData) = 0;
virtual void SetID(CString strData) = 0;
};

class CCtlBase : public ICtlBase
{
public:
virtual ~CCtlBase();
virtual void SetName(CString strData);
virtual void SetID(CString strData);

typedef void (CCtlBase::*SetFunction)(CString);

void ExeFunc(SetFunction);
};

void CCtlBase::ExeFunc(SetFunction func)
{
SetFunction setFunction = func;
this->*setFunction("aaaa");
}

class CCtlEdit : public CEdit, public CCtlBase
{
public:
virtual ~CCtlEdit();
virtual void SetText(CString strText);

void Test();
};

void CCtlEdit ::Test()
{
ExeFunc(SetText);
}

when this program run CCtlEdit::Test, this program terminate with runtime error.

but i modified this program as below.

interface IBase // empty interface
{
};

interface ICtlBase : public IBase
{
virtual ~ICtlBase(){};
virtual void SetName(CString strData) = 0;
virtual void SetID(CString strData) = 0;
};

To my surprise, modified program work well.
only ICtlBase inherits empty interface IBase.

What happened?

please Let me know this situation.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Victor Bazarov
Guest





PostPosted: Wed Aug 18, 2004 6:37 pm    Post subject: Re: What's different? (empty interface inheritance) Reply with quote



kanam wrote:
Quote:
i want to use class member function pointer.
i did as below case. but this program terminates with runtime error.

interface ICtlBase

Are you in the right newsgroup? Last time I checked 'interface'
wasn't part of C++... Did you mean 'struct'?

OK, I'll assume

#define interface struct

for the duration of this thread.

Quote:
{
virtual ~ICtlBase(){};
virtual void SetName(CString strData) = 0;

'CString' is undefined. Now I am fairly certain you're in a wrong
newsgroup. For now I'll assume

#define CString std::string

(and the appropriate headers included)

Quote:
virtual void SetID(CString strData) = 0;
};

class CCtlBase : public ICtlBase
{
public:
virtual ~CCtlBase();
virtual void SetName(CString strData);
virtual void SetID(CString strData);

typedef void (CCtlBase::*SetFunction)(CString);

void ExeFunc(SetFunction);
};

void CCtlBase::ExeFunc(SetFunction func)
{
SetFunction setFunction = func;
this->*setFunction("aaaa");

This is unnecessary. You could simply say

(this->*func)("aaaa");

Quote:
}

class CCtlEdit : public CEdit, public CCtlBase

'CEdit' is undefined here.

Quote:
{
public:
virtual ~CCtlEdit();
virtual void SetText(CString strText);

void Test();
};

void CCtlEdit ::Test()
{
ExeFunc(SetText);

'ExeFunc' expects a pointer to a member of CCtlBase. There is no member
function named 'SetText' in CCtlBase. You could only call 'ExeFunc' with
'SetName' or 'SetID' without a cast. You _could_ call it with 'SetText',
but you need a static_cast here:

ExeFunc(static_cast<SetFunction>(&CCtlEdit::SetText));

Quote:
}

when this program run CCtlEdit::Test, this program terminate with runtime error.

but i modified this program as below.

interface IBase // empty interface
{
};

interface ICtlBase : public IBase
{
virtual ~ICtlBase(){};
virtual void SetName(CString strData) = 0;
virtual void SetID(CString strData) = 0;
};

To my surprise, modified program work well.
only ICtlBase inherits empty interface IBase.

What happened?

I have no idea. I fixed your program as little as I deemed necessary,
here is the working solution:
-------------------------------------
#include <string>
#include <iostream>
using namespace std;

#define interface struct
#define CString string

interface ICtlBase
{
virtual ~ICtlBase() {}
virtual void SetName(CString strData) = 0;
virtual void SetID(CString strData) = 0;
};

class CCtlBase : public ICtlBase
{
public:
virtual ~CCtlBase() {}
virtual void SetName(CString strData)
{cout << "Name: " << strData << endl;}
virtual void SetID(CString strData)
{cout << "ID: " << strData << endl;}

typedef void (CCtlBase::*SetFunction)(CString);

void ExeFunc(SetFunction);
};

void CCtlBase::ExeFunc(SetFunction func)
{
(this->*func)("aaaa");
}

class CCtlEdit : public CCtlBase
{
public:
virtual ~CCtlEdit() {}
virtual void SetText(CString strText)
{cout << "Text: " << strText << endl;}

void Test();
};

void CCtlEdit ::Test()
{
ExeFunc(static_cast }

int main()
{
CCtlEdit e;
e.Test();
}
---------------------------------------------------------

Victor

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.