Look at the following code. It prints 123
#include <iostream.h>
class A
{
public:
A(){a = 123;}
int a;
};
class B
{
public:
A a;
int b;
};
void main()
{
B ttt;
cout << ttt.a.a << endl;
// With ttt.a
// a = 123;
// End With
}
Printable View
Look at the following code. It prints 123
#include <iostream.h>
class A
{
public:
A(){a = 123;}
int a;
};
class B
{
public:
A a;
int b;
};
void main()
{
B ttt;
cout << ttt.a.a << endl;
// With ttt.a
// a = 123;
// End With
}
If my previos reply confusesyou, and you want to assign 123 look at the following code
#include <iostream.h>
class A
{
public:
int a;
};
class B
{
public:
A a;
int b;
};
void main()
{
B ttt;
ttt.a.a = 123;
cout << ttt.a.a << endl;
// With ttt.a
// a = 123;
// End With
}
I'm not sure what your post was trying to say but, as stated before, the nearest solution would be below.
Code://#include <iostream.h> // Not part of the standard!
#include <iostream>
class A
{
public:
int a;
};
class B
{
public:
A a;
int b;
};
void main()
{
B ttt;
// With 'A'.
A &a = ttt.a;
a.a = 123;
cout << a.a << endl;
cout << ttt.a.a << endl; // Same output.
}
Then how do you change button's text, for example? I've never built win apps under C++, but I assumed, it would be the same as in VB, like ButtonName->Text = "123". No?Quote:
Originally Posted by Zaccheus
I'll illustrate the necessity of 'this' with the following code sample:Quote:
Originally Posted by JohnW@Wessex
Code:#include <iostream>
#include <conio.h>
using namespace std;
class A
{
int a;
public:
A() { a = 0; }
void SomeFun();
};
void A::SomeFun()
{
int a;
a = 123;
cout << a << " " << this->a; //those are different 'a'
}
int main()
{
A ob;
ob.SomeFun();
getch(); //to make sure it doesn't close the window
return 0;
}
All that illustrates is bad naming conventionsQuote:
Originally Posted by Neolisk
Do you always complain about conventions/standards, or just fix the code so that it works as needed with minimum changes made?
Personally I use naming conventions which make it clear whether an object is a data member or a local variable.
No.Quote:
Originally Posted by Neolisk
When assigning to a property, what actually happens there is that button.Text = "Hello" is internally seen as button.set_Text("Hello"), because reading from a property and writing to a property actually result in function calls.
The way you do it in C++ is by calling a SetText function.
Your C++ example uses public data members, which are something I would avoid in non-trivial classes.
I was talking about unnecessary this->Quote:
Originally Posted by Neolisk
If it's necessary, then use it!
(Though I don't think any of my code has a this-> in it)