|
-
July 23rd, 2008, 02:47 AM
#1
'With' keyword in C++
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:
Code:
class A
{
public:
int a;
};
class B
{
public:
A a;
int b;
};
int main()
{
B ttt;
// With ttt.a
// a = 123;
// End With
}
Thanks!
-
July 23rd, 2008, 03:00 AM
#2
Re: 'With' keyword in C++
 Originally Posted by Neolisk
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
-
July 23rd, 2008, 03:06 AM
#3
Re: 'With' keyword in C++
 Originally Posted by Paul McKenzie
not try to make code look like another language's coding style.
I feel uncomfortable when I need to write like
Code:
this->btnButton->Text =
this->btnButton->ForeColor =
this->btnButton->BackColor =
instead of (wish it worked this way)
Code:
with this->btnButton
{
Text =
ForeColor =
BackColor =
}
Isn't the last piece of code more clear and understandable?
-
July 23rd, 2008, 03:15 AM
#4
Re: 'With' keyword in C++
I think there is a similar syntax in the latest C standard (not C++). In C++ you would write a constructor:
Code:
class B
{
public:
// Constructor:
B(int inB):a(),b(inB){}
// Other functions ...
// Internal data:
private:
A a;
int b;
};
Code:
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).
Last edited by Zaccheus; July 23rd, 2008 at 03:19 AM.
-
July 23rd, 2008, 03:20 AM
#5
Re: 'With' keyword in C++
 Originally Posted by Neolisk
I feel uncomfortable when I need to write like
Code:
this->btnButton->Text =
this->btnButton->ForeColor =
this->btnButton->BackColor =
instead of (wish it worked this way)
Code:
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.
Use C++ syntax to do what you want.
Code:
SomeObject *p = this->btnButton;
p->Text = whatever;
p->ForeColor = whatever;
p->BackColor = whatever;
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
-
July 23rd, 2008, 03:37 AM
#6
Re: 'With' keyword in C++
 Originally Posted by Zaccheus
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?
 Originally Posted by Paul McKenzie
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.
-
July 23rd, 2008, 03:43 AM
#7
Re: 'With' keyword in C++
 Originally Posted by Neolisk
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++.
 Originally Posted by Neolisk
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++.
-
July 23rd, 2008, 03:51 AM
#8
Re: 'With' keyword in C++
-
July 23rd, 2008, 04:10 AM
#9
Re: 'With' keyword in C++
 Originally Posted by Zaccheus
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.
-
July 23rd, 2008, 04:16 AM
#10
Re: 'With' keyword in C++
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.
-
July 23rd, 2008, 05:38 AM
#11
Re: 'With' keyword in C++
Except in a few isolated cases, the use of this-> is usually completely unnecessary.
Code:
this->btnButton->Text =
this->btnButton->ForeColor =
this->btnButton->BackColor =
is the same as
Code:
btnButton->Text =
btnButton->ForeColor =
btnButton->BackColor =
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
July 23rd, 2008, 10:43 AM
#12
Re: 'With' keyword in C++
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
Code:
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:
Code:
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.
-
July 23rd, 2008, 08:03 PM
#13
Re: 'With' keyword in C++
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.
Last edited by srelu; July 23rd, 2008 at 08:06 PM.
-
July 23rd, 2008, 08:35 PM
#14
Re: 'With' keyword in C++
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.
quoted from C++ Coding Standards:
KISS (Keep It Simple Software):
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
Avoid magic number:
Programming isn't magic, so don't incant it.
-
July 23rd, 2008, 08:58 PM
#15
Re: 'With' keyword in C++
 Originally Posted by srelu
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|