I used to write in VB for quite long and got sticked to a 'With' keyword, which helps to keep the code clean and clear. I wonder if there's a C++ equivalent of this keyword or probably another delicate way to keep order while coding. Sample:
class A
{
public:
int a;
};
class B
{
public:
A a;
int b;
};
int main()
{
B ttt;
// With ttt.a
// a = 123;
// End With
}
Thanks!
Paul McKenzie
July 23rd, 2008, 03:00 AM
I used to write in VB for quite long and got sticked to a 'With' keyword, which helps to keep the code clean and clear. I wonder if there's a C++ equivalent of this keyword or probably another delicate way to keep order while coding.I advise against attempting to make C++ mimic another computer language.
Use the proper (i.e. normal) C++ coding paradigms and syntax that all C++ programmers understand, and not try to make code look like another language's coding style.
Regards,
Paul McKenzie
Neolisk
July 23rd, 2008, 03:06 AM
not try to make code look like another language's coding style.
I feel uncomfortable when I need to write like
with this->btnButton
{
Text =
ForeColor =
BackColor =
}
Isn't the last piece of code more clear and understandable?
Zaccheus
July 23rd, 2008, 03:15 AM
I think there is a similar syntax in the latest C standard (not C++). In C++ you would write a constructor:
class B
{
public:
// Constructor:
B(int inB):a(),b(inB){}
// Other functions ...
// Internal data:
private:
A a;
int b;
};
int main()
{
B ttt(123);
}
Constructors are better because you can execute code inside them, for example allocate extra memory when the object is created and free that memory when the object is destroyed (by writing a destructor).
with this->btnButton
{
Text =
ForeColor =
BackColor =
}
Isn't the last piece of code more clear and understandable?Not to a C++ programmer, since there is no such thing as "with" in C++. That was my whole point from the beginning. It's the first version that's understandable to a C++ programmer.
I have seen other attempts of making C++ look like other languages by using #define macros. They do nothing except obfuscate the actual code, as the program looks nothing like a C++ program.
Regards,
Paul McKenzie
Neolisk
July 23rd, 2008, 03:37 AM
I think there is a similar syntax in the latest C standard (not C++). In C++ you would write a constructor
I don't need to allocate/reallocate anything when I decide to shorten my code. It's just for visual simplicity. C code works for C++, so what's the syntax you mentioned?
Use C++ syntax to do what you want.
Seems like it's almost what I wanted. Well, now I need to get used to this. Thanks. :)
Zaccheus
July 23rd, 2008, 03:43 AM
I don't need to allocate/reallocate anything when I decide to shorten my code.
The allocation I mentioned was just an example, the code I posted shows how your example would typically be done in C++.
It's just for visual simplicity. C code works for C++, so what's the syntax you mentioned?
C and C++ are slowly moving apart, the latest C standard contains things which are not supported in C++.
Zaccheus
July 23rd, 2008, 03:51 AM
It's C99, you can read about it here:
http://www.codeguru.com/forum/showthread.php?t=455270
with a link to here:
http://david.tribble.com/text/cdiffs.htm#C99-desig-init
Neolisk
July 23rd, 2008, 04:10 AM
the latest C standard contains things which are not supported in C++.
Didn't know that, really. Then I think I'd better stick to Paul's suggestion, it's the most clear by now.
Zaccheus
July 23rd, 2008, 04:16 AM
I've just noticed that you are talking about changing the properties of an object after the object had been created, sorry for my misunderstanding.
JohnW@Wessex
July 23rd, 2008, 05:38 AM
Except in a few isolated cases, the use of this-> is usually completely unnecessary.this->btnButton->Text =
this->btnButton->ForeColor =
this->btnButton->BackColor =is the same as
btnButton->Text =
btnButton->ForeColor =
btnButton->BackColor =
Notsosuperhero
July 23rd, 2008, 10:43 AM
I have to agree with Paul. Don't try to mimic another language. I don't really use VB(I haven't used it for years) and I have no idea what 'with' is supposed to mean.
All you have to do to change a public member is access it.
in the case of your example you'd just have to do ttt.a.a = 323, provided the a class variable in B is valid. Alternatively you could make use of inheritance by making B a child class of A
class A
{
public:
int a;
};
class B : public A
{
public:
int b;
};
class B will still contain the int variable 'a' it just extends upon class A. The ': public A' just specifies that you want the class to be a parent of A with public access. Now class B would be able to access anything that is marked public or protected from A. So you can do:
int main()
{
B ttt;
ttt.a = 32;
return 0;
}
May not be super clean, but remember C++ is an intense language, as opposed to VB which is relatively more easily readable. Good thing though, you can hide your classes in header files and define the class's functions in C/C++ source files.
srelu
July 23rd, 2008, 08:03 PM
C++ is without With.
If you think "this->btnButton->ForeColor = ..." is too long, you never saw a complex C++ source code.
How about a pointer written on 2-3 lines ?
You can simplify the writing by using something like:
CButton* b = this->btnButton;
then use:
b->ForeColor = ...
but this method will make the generated code longer and slower because a new pointer named b is created and used.
Kheun
July 23rd, 2008, 08:35 PM
Actually, I like comparing one programming language with another to understand their differences, strength and weakness. I agree that the "with" keyword will be very useful in the old time when everyone is still using edlin or vi. However, with today's IDE, it is not so important as most are equipped with intellisense and help to autocomplete our code. So it is less likely to misspell.
Anyway, as already shown in Paul's excellent example, you can assign it to a temporary pointer to reduce typing. :)
Paul McKenzie
July 23rd, 2008, 08:58 PM
You can simplify the writing by using something like:
CButton* b = this->btnButton;
then use:
b->ForeColor = ...
but this method will make the generated code longer and slower because a new pointer named b is created and used.It makes the code longer, but a good optimizer sees such things, so the code is more than likely not slower.
Regards,
Paul McKenzie
Peter_APIIT
July 24th, 2008, 03:17 AM
Writing VB style using C++ is a shame to you and this violate C++ god programming techniguqes and pricinples.
You better familiar yourself with C++ rather than request a shortcut.
Zaccheus
July 24th, 2008, 03:34 AM
with this->btnButton
{
Text =
ForeColor =
BackColor =
}
The thing is, in a typical C++ program such data would be private.
You would not be thinking about the individual data items, but about the button as a whole. Instead of writing to individual data members, you would have functions which change the state of the button:
I have not come across keyword "with" in C++ associated with structures and class.
Even if it is provided I will avoid using it.
struct MyStruct
{
int nAge;
int nGrade;
};
void Function()
{
MyStruct obj;
int nAge;
int nTempGrade;
/* will below code result in obj.nAge = nAge or nAge = obj.nAge = nAge */
with obj
{ /* or begin */
nTempGrade = nGrade;
nAge = nAge;
} /* or end */
}
Neolisk
July 24th, 2008, 10:32 AM
Except in a few isolated cases, the use of this-> is usually completely unnecessary.
A taste-related thing, depends on the programmer. Also helps resolving unlikely ambiguity for intellisense.
If you think "this->btnButton->ForeColor = ..." is too long, you never saw a complex C++ source code.
I have actually, but it doesn't help.
You better familiar yourself with C++ rather than request a shortcut.
You know, it's man's privilege to develop new things through a perspective of what he already knows. Well, that's exactly what I'm trying to do.
The thing is, in a typical C++ program such data would be private.
Have you ever worked with a Button class? :)
exterminator
July 24th, 2008, 12:24 PM
The thing is, in a typical C++ program such data would be private.It is probably private in VB as well. Those are properties. All this if I recall what I used to do 5 years ago. :)
Zaccheus
July 24th, 2008, 01:11 PM
Have you ever worked with a Button class? :)
Yes, in fact I've helped to write one which is used in commercial software. ;)
It is probably private in VB as well. Those are properties. All this if I recall what I used to do 5 years ago. :)
In that case the 'with' construct would not work anyway, because C++ does not have properties.
Closest you're going to get, but this sort of thing can certainly clarify code in many situations.
Neolisk
July 24th, 2008, 01:45 PM
I just remembered VB code designer generated code, which is 'friend withevents...'. So yes, they might be private and still be able to use directly. Actually, I don't know exactly if they are. But it doesn't depend on whether they are properties or not. And it certainly doesn't depend on whether you're using VB or C. Well, I doubt any of them uses a different Button class apart from what's defined in Windows.
Lindley: I already understood the possible solution, originally suggested by Paul, so there is not need to duplicate it.
Zaccheus: Well, I just asked, because I found it curious that you didn't refer to the button directly as it's the usual way for me. So I write like ButtonName.Text = "123", and not something like ButtonName.SetText("123", enumJustPlainText, enumNoDontDisplayMessageBoxes, ... other things) to add text to a button. :)
Lindley
July 24th, 2008, 02:01 PM
Lindley: I already understood the possible solution, originally suggested by Paul, so there is not need to duplicate it.
His example used a pointer, I believe. I was merely demonstrating that references work just as well in these cases.
souldog
July 24th, 2008, 02:01 PM
Lindley: I already understood the possible solution, originally suggested by Paul, so there is not need to duplicate it.
There is a subtle difference in that Lindley is using a reference instead of a pointer,
which is probably better if you want to create an alias to a member.
Lindley
July 24th, 2008, 02:02 PM
See, everyone's always repeating each other....no big.
Neolisk
July 24th, 2008, 02:21 PM
Sorry, didn't notice the difference. :)
Thanks for the hint.
JohnW@Wessex
July 25th, 2008, 03:25 AM
A taste-related thing, depends on the programmer.We made removal of unnecessary this-> part of our coding standards. People weren't using it for any good reason apart from getting intellisense to list the class members and then forgetting to delete the this-> part afterwards.
Also I had the job of writing the standards! :D. Nobody else minded the rule.
souldog
July 25th, 2008, 03:38 AM
I have to agree, If I had to read someones code with this-> all over the place and unnecessary alias, I would be annoyed
Zaccheus
July 25th, 2008, 03:40 AM
I just remembered VB code designer generated code, which is 'friend withevents...'. So yes, they might be private and still be able to use directly. Actually, I don't know exactly if they are. But it doesn't depend on whether they are properties or not.
It matters a lot. You never want to be directly manipulating the data members in a non-trivial class. Properties are get and set functions with a nicer syntax, which means you are not directly manipulating the data members.
And it certainly doesn't depend on whether you're using VB or C. Well, I doubt any of them uses a different Button class apart from what's defined in Windows.
C++ does not have properties, so it would certainly depend on what you are using.
What's defined in Windows is the Windows API, which is very different from the VB Button example you gave.
Zaccheus: Well, I just asked, because I found it curious that you didn't refer to the button directly as it's the usual way for me. So I write like ButtonName.Text = "123", and not something like ButtonName.SetText("123", enumJustPlainText, enumNoDontDisplayMessageBoxes, ... other things) to add text to a button. :)
Like I said above, C++ does not have properties.
s_kannan55
July 25th, 2008, 04:36 AM
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
}
s_kannan55
July 25th, 2008, 04:39 AM
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
}
JohnW@Wessex
July 25th, 2008, 05:19 AM
I'm not sure what your post was trying to say but, as stated before, the nearest solution would be below.
//#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;
Like I said above, C++ does not have properties.
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?
We made removal of unnecessary this-> part of our coding standards.
I'll illustrate the necessity of 'this' with the following code sample:
#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;
}
souldog
July 25th, 2008, 06:24 AM
I'll illustrate the necessity of 'this' with the following code sample:
#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 conventions
Neolisk
July 25th, 2008, 07:18 AM
Do you always complain about conventions/standards, or just fix the code so that it works as needed with minimum changes made?
Zaccheus
July 25th, 2008, 08:39 AM
Personally I use naming conventions which make it clear whether an object is a data member or a local variable.
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?
No.
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.
JohnW@Wessex
July 25th, 2008, 08:46 AM
I'll illustrate the necessity of 'this' with the following code sampleI was talking about unnecessary this->
If it's necessary, then use it!
(Though I don't think any of my code has a this-> in it)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.