Welcome, Guest. Please login or register.

Author Topic: question about C++ constructors  (Read 23012 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show all replies
    • http://members.shaw.ca/mveroukis/
question about C++ constructors
« on: January 23, 2003, 09:42:41 PM »
Hey,

So I have this class which allocates a bunch of stuff in a constructor.  Now, let's say one of those allocations fails, what's the best way to gracefully back out of this?  Should I throw an exception?  But this would require every time an object is allocated to be surrounded by a try{}catch block.  How would this work with statically allocated classes?!?  Seems like this isn't the best way to do this.  I can't think of any better way which makes me think that perhaps it's a bad idea to allocate resources that might fail in a constructor.  Opinions?!?

  - Mike
YOU ARE NOT IMMUNE
 

Offline GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show all replies
    • http://members.shaw.ca/mveroukis/
Re: question about C++ constructors
« Reply #1 on: January 23, 2003, 10:36:28 PM »
Quote
There is a saying when it comes to good programming: Allocate late, release early, i.e. you should allocate resources when you need them and release them as soon as possible. You should not allocate stuff in the constructor unless you absolutely have to.

Thanks for the reply.

My plan was to create a class that opens a group of windows.  The windows all get used together, I just wanted one single class to control them all as they would be acted upon as one entity.  These windows kinda need to be around so I can't use that principle.  However, I might just make a special method to open the windows and one to close them.

  - Mike
YOU ARE NOT IMMUNE
 

Offline GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show all replies
    • http://members.shaw.ca/mveroukis/
Re: question about C++ constructors
« Reply #2 on: January 24, 2003, 11:16:33 PM »
@Dave,

your comments on smart/auto pointers via the Reference template is very interesting.  let me see if I got it straight.

By using your Reference template you can dynamically allocate classes and be assured that when the reference goes out of scope the referenced class will then be deallocated - assuming there was only one reference to begin with.  I assume the destructor for the Reference template class would delete the wrapped object?  Is this correct???  Can I have the source to this? It might more sense if I just look at the code.

As for Design Patters, I think I need to pick up a copy.  Although I have Bjarne Stroustrup's C++ book it's the 2nd edition and doesn't mention Factories or other patterns.  I did however find some info on factories and the Signleton pattern and it might be useful to me.  The singleton pattern makes sense as I'll be opening a set of windows and there will never be more instances of these windows open.  They will also be global, the only problem is I'm not sure how to make it automatically deallocate itself in C++.  Anyway, I'll have to check out that book and see what it has to offer.  Off to Chapters I go!  :-)

  - Mike
YOU ARE NOT IMMUNE
 

Offline GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show all replies
    • http://members.shaw.ca/mveroukis/
Re: question about C++ constructors
« Reply #3 on: January 24, 2003, 11:17:52 PM »
Oh btw, I'm using SAS C 6.57 which means I can't use templates.  :-(  So I may not be able to use your code directly but I would still like to look at the code for my own educational purposes.  :-)

  - Mike
YOU ARE NOT IMMUNE
 

Offline GlaucusTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 4518
    • Show all replies
    • http://members.shaw.ca/mveroukis/
Re: question about C++ constructors
« Reply #4 on: January 25, 2003, 07:06:45 PM »
Okay, thanks a lot Dave!  I'll take a close look at it!

  - Mike
YOU ARE NOT IMMUNE