如何访问父类的变量 [英] How do I access to variable on my parent class

查看:58
本文介绍了如何访问父类的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MFC应用程序上有一个Dialog

I have a Dialog on MFC application

MyDialog :
{
 int variable1;
 int variable2;
 Class1 cls = new Class1();
}









在Class1中,有一些功能1





In Class1, have some function1

Class1()
{
  void funtion1();
}





如何访问并返回 Class1 :: Function1()中的variable1





How to Access and return to variable1 in Class1::Function1()

Class1::Function1()
{
  MyDialog dlg = new MyDialog ();
  int x = dlg->variable1; //if like this, variable1 alway=0, because in above line, i'm define new myDialog()
 dlg->variable2 = x;
}







我认为委托.NET但在MFC应用程序中,我可以要完成吗?




I think to delegate on .NET but in MFC application, I can't get it done ?

推荐答案

只要您的代码充满了严重的编译错误,就没有必要讨论如何解决语义问题!很难说哪些是你描述的语义问题的一部分,或者我们是否应该简单地忽略你的整个代码,因为它无论如何都不是文字副本。



请先完成并编译代码。然后在此处复制并粘贴该代码 - 您实际编译的代码。只需修复编译器错误,您至少可以解决问题的一半。在那之后,实际修复将是一个小的改变(理查德在他的评论中描述)。但是现在,我们需要重写你的整个代码才有意义。





PS:只是一个大小提示开始,因为我注意到你提到.NET:

除非必须,否则不要使用new!了解堆的差异(动态分配新)和堆栈(没有动态分配);无论何时你都喜欢堆栈!



这与你的问题无关,但它会解决一些编译错误。
There is no point discussing how to solve a semantic problem as long as your code is so riddled with severe compiler errors! it's hard to tell which of those are part of the semantic problem that you describe, or whether we should simply ignore your entire code because it is not a literal copy anyway.

Please complete and compile your code first. Then copy and paste that code here - the code that you actually compiled. Just by fixing the compiler errors you'll solve at least half of the problem. After that, the actual fix will be a minor change (what Richard described in his comment). Right now however, we'd need to rewrite your entire code just for it to make sense.


P.S.: just one big tip to get started, as I've noticed you mentioned .NET:
Don't use new unless you have to! Learn the difference of the heap (dynamically allocating with new) and the stack (no dynamic allocation); prefer the stack whenever you can!

This has nothing to do with your problem, but it will solve some of the compiler errors.


不,不清楚。你的代码没有多大意义。您创建一个新对话框并立即尝试使用尚未初始化的变量。您还尝试在外部类中创建一个新对话框。

您需要更多类似的东西:

No it's not clearly. Your code does not make much sense. You create a new dialog and immediately try to use a variable that has not been initialized. You also try to create a new dialog inside your external class.
You need something more like:
MyDialog :
{
 int variable1;
 int variable2;
 Class1* cls = new Class1();

 variable1 = < some value >
 variable2 = cls->function1(variable1);
}

// ...
Class1()
{
  int funtion1(int value)
  {
      int newvalue;
      // add code to use the input to create the result
      return newvalue;
  }
}


这篇关于如何访问父类的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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