关于可见性的基本问题 [英] Fundamental question about visibility

查看:73
本文介绍了关于可见性的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是基本的我相信,但它已经取笑我一段时间了:


我的应用程序中有两个课程。第一个类被实例化为我的第二个类的

的成员。在第一个类中,方法(事件)需要能够调用第二个类的方法。使用静态类可能,但这是不可取的。显然有一些可见性问题我不熟悉b $ b熟悉。这不是一个亲子关系,因为它没有

enheritance吗? (如果是这样,像GetParent().WantToBeCalled()将

是正确的方式我猜...)

class FirstClass {

public:

void TriggeringEvent(); //当调用它时,调用

WantToBeCalled of CS

};


class SecondClass {

public:

FirstClass FS;

void WantToBeCalled(); //

};


SecondClass CS;

....


什么是基本的可见性问题?我不明白吗?我已经看到出口了吗?指令以及可疑的命名空间可能与它有关,但是我感到厌倦或反复试验而没有成功的




在此先感谢,

Casper

My question is fundamental I beleive but it has been teasing me for a while:

I have two classes in my app. The first class is instantiated as a member of
my second class. Within this first class, a method (event) needs to be able
to invoke methods of the second class. With static classes its possible but
this is not desirable. There''s obviouly some visibility problem I am not
familiar with. It is not a parent-child relationship since there''s no
enheritance is it? (If so, something like GetParent().WantToBeCalled() would
be the right way I guess...)
class FirstClass{
public:
void TriggeringEvent(); // When this is called, invoke
WantToBeCalled of CS
};

class SecondClass{
public:
FirstClass FS;
void WantToBeCalled(); //
};

SecondClass CS;
....

What fundamental "visibility issue" haven''t I understood correct? I''ve
looked at "export" directives and also suspect namespaces could have
something to do with it but I am getting tired or trial-and-error without
success.

Thanks in Advance,
Casper

推荐答案

Casper Bang写道:
Casper Bang wrote:
我的问题是基本的我相信但它一直在取笑我
同时:

我的应用程序中有两个课程。第一个类被实例化为我的第二个类的
成员。在第一个类中,方法(事件)需要能够调用第二个类的方法。


C ++中没有方法。有会员功能。有些人称之为b / b
cirtual成员函数方法,但由于这个含义并不完全已知,所以不建议将C ++成员函数称为

a C ++语言讨论。简单地说,人们会误解b
。例如,你的示例代码不包含那些只调用虚拟成员函数方法的那些方法。

使用静态
类可能但这不是可取的。有一些我不熟悉的可见性问题。这不是一个亲子关系,因为它没有继承吗? (如果是这样,像GetParent()那样的东西.WantToBeCalled()将是正确的方式我猜...)
[SNIP]什么是基本的可见性问题我不明白是对吗?
My question is fundamental I beleive but it has been teasing me for a
while:

I have two classes in my app. The first class is instantiated as a
member of my second class. Within this first class, a method (event)
needs to be able to invoke methods of the second class.
There are no methods in C++. There are member functions. Some people call
cirtual member functions methods, but since this meaning is not all well
known, it is not recommended to refer to C++ member functions as methods in
a C++ language discussion. Simply it can happen that people will
misunderstand. For example your example code did not contain any method for
those, who only call virtual member functions methods.
With static
classes its possible but this is not desirable. There''s obviouly some
visibility problem I am not familiar with. It is not a parent-child
relationship since there''s no enheritance is it? (If so, something
like GetParent().WantToBeCalled() would be the right way I guess...) [SNIP] What fundamental "visibility issue" haven''t I understood correct?



[SNIP]


编号我必须说你似乎还没有理解面向对象

设计。你上面想做的事情可以通过一些技巧完成,但是我不确定它必须是什么。你能描述一下你想要做什么吗?在你的代码中我没有看到任何概念 - 不是你没有

概念,但是示例类的名称太模糊了,不知道它们是什么。


-

WW aka Attila


[SNIP]

No. I must say that it seems that you have not understood object oriented
design. What you wanted to do above can be done with some "tricks", but I
am not exactly sure that it has to be. Could you please describe what you
really want to do? In your code I see no concepts - not that you have no
concept, but the names of the example classes are too vague to have any idea
about what they are.

--
WW aka Attila


有没有C ++中的方法。有成员函数
是的,我记得以前听说过,但是我假设在C ++ class {}构造中说明了这样的原型

,称之为方法,函数或

程序在语义上是相同的,与我原来的
问题几乎没有关系。

否。我必须说你似乎还没有理解面向对象的问题设计。
实际上,这可能是部分真实的,因为混合了编程真实的OO

Java和假OO Visual Basic 3 - 4年。因此,混合范式C ++是完全新的问题。

上面你想做的事情可以通过一些技巧完成,但我不是确切地说它必须是。你能描述一下你真正想做的事吗?在你的代码中,我没有看到任何概念 - 不是你没有
概念,但是示例类的名称太模糊了,没有任何关于它们的
的想法。
There are no methods in C++. There are member functions Yeah I remember hearing that before, however I assume when such a prototype
is illustrated within a C++ class{}construct, calling it method, function or
procedure is semantically the same and hardly relevant to my original
question.
No. I must say that it seems that you have not understood object oriented
design. That may be partly true practically, due to a mix of programming real OO
Java and fake OO Visual Basic for 3-4 years. The hybrid paradigm C++ is thus
a completely new matter.
What you wanted to do above can be done with some "tricks", but I
am not exactly sure that it has to be. Could you please describe what you
really want to do? In your code I see no concepts - not that you have no
concept, but the names of the example classes are too vague to have any idea about what they are.




如果我在我的例子中更加明确,人们会批评这是一个没有处理MSVC ++或MFC库的

ANSI C ++组但是这里是

没有:


//覆盖CWndApp,定义我们的主应用程序对象

//这是我们的入口点,在那里覆盖的InitInstance

//方法负责创建窗口等。

class CMyApp:public CWinApp

{

public:

virtual BOOL InitInstance();

};

//覆盖CEdit类,这样我们就可以捕获CTRL + ENTER活动

class CModifiedEdit:public CEdit

{

public:

afx_msg void OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags);

};

//覆盖CWnd,d在我们的主窗口中,应用程序将创建

class CMainWindow:public CWnd

{

public:

CMainWindow( );

void OnParseMe();

CModifiedEdit m_wndEdit;

};

在CMyApp中,CMainWindow被实例化。 CMainWindow有会员

函数:: OnKeyDown()迟早会被解雇(事件回调)。

当这种情况发生时,我有兴趣调用成员函数

CMainWindow :: OnParseMe()


希望这会有所帮助,

Casper



Had I been more specfic in my example, people would criticize that this is a
ANSI C++ group not dealing with MSVC++ or the MFC library but here goes
nothing:

// Overide CWndApp, defining our main application object
// This is our "Point of entry", in that the overidden InitInstance
// method is responsible for creating window(s) etc.
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// Overide the CEdit class so we can capture CTRL + ENTER events
class CModifiedEdit : public CEdit
{
public:
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};
// Overide CWnd, defining our main window the application will create
class CMainWindow : public CWnd
{
public:
CMainWindow();
void OnParseMe();
CModifiedEdit m_wndEdit;
};
Within CMyApp the CMainWindow is instantiated. CMainWindow has member
function ::OnKeyDown() which will be fired (event callback) sooner or later.
When this happens, I am interested in calling the member funtion
CMainWindow::OnParseMe()

Hope this helps,
Casper


Casper Bang写道:
Casper Bang wrote:
C ++中没有方法。有成员函数是的,我记得以前听过,但是我假设当这样的原型在C ++类{}构造中被说明时,称之为
方法,函数或过程在语义上是相同的并且几乎不存在与我原来的问题相关。
There are no methods in C++. There are member functions Yeah I remember hearing that before, however I assume when such a
prototype is illustrated within a C++ class{}construct, calling it
method, function or procedure is semantically the same and hardly
relevant to my original question.




这与queston无关,但是你问他的方式。 :-)



It is not relevant to the queston, but he way you have asked it. :-)

不。我必须说你似乎还没有理解面向对象的设计。
No. I must say that it seems that you have not understood object
oriented design.


That may be partly true practically, due to a mix of programming real
OO Java and fake OO Visual Basic for 3-4 years. The hybrid paradigm
C++ is thus a completely new matter.




我建议您掌握(一旦掌握了C ++基金会)

这本名为Multi-Paradigm DESING for C ++的书来自Coplien?

如果我在我的例子中更加特别,人们会批评这是一个ANSI C ++组而不是处理MSVC ++或MFC库
但是这里什么都没有:


;-)

//覆盖CWndApp,定义我们的主应用程序对象
//这是我们的入口点,因为覆盖的InitInstance
//方法负责创建窗口等。
类CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};

//覆盖CEdit类,以便我们可以捕获CTRL + ENTER事件
类CModifiedEdit:public CEdit
{
public:
afx_msg void OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags);
};

//覆盖CWnd,定义我们的主窗口应用程序将创建
类CMainWindow:public CWnd
{
公共:
CMainWindow();
void OnParseMe();
CModifiedEdit m_wndEdit;
};

在CMyApp中,CMainWindow被实例化。 CMainWindow有成员
function :: OnKeyDown(),它将在稍后或稍后触发(事件回调)。当发生这种情况时,我有兴趣打电话给会员
功能CMainWindow :: OnParseMe()



May I suggest you getting hold of (once you have mastered C++ fundmentals)
the book with the title Multi-Paradigm DESING for C++ from Coplien?
Had I been more specfic in my example, people would criticize that
this is a ANSI C++ group not dealing with MSVC++ or the MFC library
but here goes nothing:
;-)
// Overide CWndApp, defining our main application object
// This is our "Point of entry", in that the overidden InitInstance
// method is responsible for creating window(s) etc.
class CMyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
// Overide the CEdit class so we can capture CTRL + ENTER events
class CModifiedEdit : public CEdit
{
public:
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};
// Overide CWnd, defining our main window the application will create
class CMainWindow : public CWnd
{
public:
CMainWindow();
void OnParseMe();
CModifiedEdit m_wndEdit;
};
Within CMyApp the CMainWindow is instantiated. CMainWindow has member
function ::OnKeyDown() which will be fired (event callback) sooner or
later. When this happens, I am interested in calling the member
funtion CMainWindow::OnParseMe()




嗯。这似乎是MFC设计中的一个流程。编辑控件确实

很好地知道Window拥有它(父级),但似乎无法获得属于该窗口的C ++对象的
。真的吗?你确定

你不能要求MFC给你父C ++对象吗?如果是这样的话,那么我能想象的唯一简单的C ++就是添加一个非默认的构造函数来支持
CModifiedEdit获取指向其父C ++对象的指针并在其上初始化它

CMainWindow构造函数的初始化列表:


class CMainWindow;


//覆盖CEdit类所以我们可以捕获CTRL + ENTER事件

class CModifiedEdit:public CEdit

{

CMainWindow * parent;

public :

CModifiedEdit(CMainWindow * mamma):parent(mamma){; }

afx_msg void OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags);

};

//在CModifiedEdit.cpp中的某处,很远的

//或在课堂声明后的CModifiedEdit.h和

//包括CMainWindow.h以启用内联

/ /(不要忘记标题守卫!):

afx_msg void CModifiedEdit :: OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)

{

//做任何事情

parent-> OnParseMe();

}


//覆盖CWnd,定义我们的主窗口应用程序将创建

class CMainWindow:public CWnd

{

public:

CMainWindow() ;

void OnParseMe();

CModifiedEdit m_wndEdit;

};


CMainWindow :: CMainWindow():m_wndEdit(this){

//其余的

}


===


< OT>

当然还有Windows Way [TM]。将OnParseMe()

的执行绑定到用户定义的Windows消息。这样,任何知道主要的
窗口句柄的人都可以要求它使用SendMessage重新分析

(同步)或Postmessage(异步)。

< / OT>


-

WW又名阿提拉



Hmmm. That seems to be a flow in the MFC design. The edit control does
knwow it very well what Window owns it (parent) but it seems it has no way
to get the C++ object belonging to that Window. Is that true? Are you sure
you are unable to ask MFC to give you the parent C++ object? If this is so
the only easy C++ thing I can imagine is to add a non-default constructor to
CModifiedEdit taking a pointer to its parent C++ object and initialize it on
the initializer list of the CMainWindow constructor:

class CMainWindow;

// Overide the CEdit class so we can capture CTRL + ENTER events
class CModifiedEdit : public CEdit
{
CMainWindow *parent;
public:
CModifiedEdit(CMainWindow *mamma) : parent(mamma) { ; }
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
};
// Somewhere in a CModifiedEdit.cpp, far far away
// or int the CModifiedEdit.h after the class declaration and
// including CMainWindow.h to enable inlining
// (don''t forget header guards!):
afx_msg void CModified:OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// Do whatever
parent->OnParseMe();
}

// Overide CWnd, defining our main window the application will create
class CMainWindow : public CWnd
{
public:
CMainWindow();
void OnParseMe();
CModifiedEdit m_wndEdit;
};

CMainWindow::CMainWindow():m_wndEdit(this) {
// The rest
}

===

<OT>
And of course there is a Windows Way[TM]. Bind the execution of OnParseMe()
to a user defined Windows Message. In that way anyone knowing the main
window handle will be able to ask it to reparse using a SendMessage
(synchronous) or Postmessage (asynchronous).
</OT>

--
WW aka Attila


这篇关于关于可见性的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆