|
It may point to anything. Inner or outer, it's a bad practice. It's highly recommended not to use it.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
VuNic wrote: It may point to anything. Inner or outer, it's a bad practice. It's highly recommended not to use it.
See this article for detailed descriptions: Compilation Firewalls[^].
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
Good link, I will study it.
regards,
George
|
|
|
|
|
That deals with just one case. But the article has not talked about the dangerous side of it. If you are have hard pointers like that, there's heck a lot of chances that you'll end up in memory leaks. If the program is 21 odd lines, then there's no problem. But in an application that has 40-50 thousand lines, if you are implementing hard pointers like this, that's it. It's an assured dissaster. I guess opaque pointers fit more better for the raw C guys and not C++. I'll give a little example here:
<br />
class MyApp<br />
{<br />
<br />
MyGun* pmygun;<br />
MyBomb* pBomb;<br />
MyMissle* pMissile;<br />
.<br />
.<br />
.<br />
<br />
}<br />
MyApp()<br />
{<br />
}<br />
<br />
LoadWeapon(Handle,Type)<br />
{<br />
}<br />
<br />
UnloadWeapon(Handle,Type)<br />
{<br />
<br />
}<br />
<br />
<br />
~MyApp()<br />
{<br />
}<br />
And sometime back my mate had such a pointer(pobj) in his MyAppclass, and in the destructor he kept calling pobj->release() without checking if it's been initialized or not. In most of the circumstances it got initialized and it went fine, but a tricky flow got thru these initializing conditions and when the destructor of the MyApp class got called, it crashed the application. This is just one example . I've seen a lot more like that.
I wish to suggest something called the smart-pointers.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Thanks VuNic,
The issues you described above are the common issues with raw pointers, are there any special new issues you described above special for opaque pointers?
regards,
George
|
|
|
|
|
But I think it's basic responsibility to check validity of pointers before using them to do release job.
~Foo() {
if(m_pG) {
m_pG->Release();
m_pG = NULL;
}
}
Maxwell Chen
|
|
|
|
|
Thanks VuNic,
Bad practice you mean pointing to outer or using opaque pointer?
regards,
George
|
|
|
|
|
See HERE[^].
Maxwell Chen
|
|
|
|
|
Hi Maxwell,
How do you pronounce the word "Pimpl"?
regards,
George
|
|
|
|
|
George_George wrote: How do you pronounce the word "Pimpl"?
I don't know. But "PIMPL" is a widely seen term in C++. You can google with this keyword "PIMPL".
(No 'e' at the tail.)
Maxwell Chen
|
|
|
|
|
I got it. "Pimpl" is "Private implementation" for short.
Maxwell Chen
|
|
|
|
|
Thanks Maxwell,
regards,
George
|
|
|
|
|
Hello everyone,
When implementing a general template class, sometimes we call T() -- suppose T is type argument of a template class.
My questions,
1. what will happen if T is POD type? Do nothing?
2. Is it good code? Or working but not good code?
Here is my test code, works in MSVC 2008.
template <class T> class Foo {
public:
void static test()
{
T();
}
};
int main()
{
Foo<int> g;
g.test();
return 0;
}
thanks in advance,
George
|
|
|
|
|
I've got VC6 open at the moment, and the "constructor" for an int returns a zero-initialized int. Change test to:
T test() { return T(); }
and look at the assembly. You can also do:
int x = int();
|
|
|
|
|
Thanks Michael,
Do you think it is C++ standard to initialize POD type to zero?
regards,
George
|
|
|
|
|
I deduce yes, by looking at the disassembly. But I wouldn't trust VC6 when it comes to current standards. Try with VC8 or 9 or check out web pages about the standard like this one[^]
|
|
|
|
|
Thanks Michael,
Could you let me know which part of the wiki page do you refer which relates to my question please? It is too long.
regards,
George
|
|
|
|
|
|
Hi Mike,
What you referred is a Google search result. What I am asking is which part of the standard do you think relates to my question?
regards,
George
|
|
|
|
|
Well, I don't have the entire spec memorized. I did some initial research, and gave you a starting point for you to find out the answer yourself.
|
|
|
|
|
Thanks Mike,
I find this stuff.
http://www.codeguru.com/forum/showthread.php?p=1695187&postcount=9[^]
Seems this statement "otherwise, the object is zero-initialized" in post #9 covers this topic. Any comments?
But do you think,
T t = T(); and T t; are the same? I think the standard only covers the 1st case, for T t it is undefined value?
regards,
George
|
|
|
|
|
Hello everyone,
Even if I have tested with MSVC 2008 that no resource leak, I want to confirm with you whether it is good code to maintain resource and avoid resource leak. Also whether the code is dependent on some non-Spec regulated points, e.g. MSVC 2008 specific things.
The design pattern is, embed a sub-object into another object (e.g. embed Goo object g into Foo object), but in destructor of Foo, destructor of its sub-object (e.g. Goo object g) is not called explicitly.
I have tested destructor of Goo object g will be called in MSVC 2008, but I am not sure whether we could rely on this -- when object goes out of scope, its sub-object also goes out of scope and destructor always gets called?
#include <iostream>
using namespace std;
class Goo {
public:
Goo()
{
cout << "constructing Goo" << endl;
}
virtual ~Goo()
{
cout << "destructing Goo " << endl;
}
};
class Foo {
public:
Goo g;
Foo (Goo _g) : g (_g)
{
cout << "constructing Foo " << endl;
}
virtual ~Foo()
{
cout << "destructing Foo " << endl;
}
};
int func()
{
Goo g;
Foo f (g);
return 0;
}
int main()
{
func();
return 0;
}
thanks in advance,
George
|
|
|
|
|
Yes it gets called automatically. What you've done is a composition and it is Not bad at all, but this is :
Goo* g;
If you are using a raw pointer/heap-allocate inside your "container" class, then it's unsafe. In the sense, you need to manually checks if it's get getting deleted properly.
OK,. what country just started work for the day ? The ASP.NET forum is flooded with retarded questions. -Christian Graus
Best wishes to Rexx[^]
|
|
|
|
|
Thanks VuNic,
Question answered.
regards,
George
|
|
|
|
|
George_George wrote: The design pattern is, embed a sub-object into another object (e.g. embed Goo object g into Foo object),
Where did you get the code for this design pattern.
|
|
|
|