C++ Learning Community Forum
August 01, 2010, 02:58:47 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Hello. Smiley
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: what every constructor should have  (Read 1129 times)
*brammie*
my rank:
C++ Freak
***
Posts: 309



View Profile WWW
« on: August 14, 2008, 02:47:46 PM »

all of your classes should have this in their constructor...
i've been searching for a bug for 3 hours, and this solved it  Grin (thanks to the debugger)
Code
GeSHi (cpp):
class Foo
{
   public:
   Foo();
}
 
Created by GeSHI 1.0.7.18
Code
GeSHi (cpp):
Foo::Foo()
{
   memset((void*)this,0,sizeof(Foo));
}
 
Created by GeSHI 1.0.7.18
Logged
adeyblue
Dr. of C++ology
****
Posts: 653

Taming the turntables a beat at a time


View Profile WWW
« Reply #1 on: August 14, 2008, 04:30:37 PM »

Well, it'll probably work if the class contains only POD types. If it contains anything nontrivial like a non-POD class or derives from or is a virtual base class then all you've done is substituted one bug for another that may not manifest itself so readily. Don't be so lazy and initialize the contents to sane default values in the initialization list and you're golden no matter what type of class you have.
Logged

myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #2 on: August 14, 2008, 04:46:35 PM »

all of your classes should have this in their constructor...
Code
GeSHi (cpp):
Foo::Foo()
{
   memset((void*)this,0,sizeof(Foo));
}
Created by GeSHI 1.0.7.18

Absolutely not.
You are running all over the memory without regard for its type.
What you should do is correctly initalize every member variable in every constructor. Preferably in the initializer list.
Logged
myork
Global Moderator
C++ guru
*****
Posts: 1147


View Profile
« Reply #3 on: August 14, 2008, 04:51:20 PM »

i've been searching for a bug for 3 hours, and this solved it  Grin (thanks to the debugger)

It does not look like you solved the problem. Just covered up the problem.
What happens when you suddenly decide that Foo must now inherit from a base class. BOOOOOOM
Logged
charlie
Is the meadow on fire?
Dr. of C++ology
****
Posts: 730


CsGYh8AacgY


View Profile
« Reply #4 on: August 14, 2008, 06:26:10 PM »

Oops! Grin

Steps to posting a C++ Tip:
  • Post in C++ Help or C++ General and ask if your solution is a good one.
  • If you don't get several responses in the first few hours saying that it's a horrible idea, then consider posting it as a tip.

Wink You've got to be careful about solutions that appear to solve the immediate problem. Sometimes the only way to know its wrong is by asking around and learning from experience.
Logged
*brammie*
my rank:
C++ Freak
***
Posts: 309



View Profile WWW
« Reply #5 on: August 15, 2008, 09:30:54 AM »

umm.. ok Undecided
(thought i had finally found something useful Tongue)
Logged
charlie
Is the meadow on fire?
Dr. of C++ology
****
Posts: 730


CsGYh8AacgY


View Profile
« Reply #6 on: August 17, 2008, 02:47:49 AM »

(thought i had finally found something useful Tongue)

Don't worry about it. You did! Now you know to pay attention to type and be careful of those C functions when working with C++ classes.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!