Thread: Double constructing of a class

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    30

    Double constructing of a class

    Ok, first of all, nice forum .. << i'm new here.
    Now my question... i got my class: cCountryList. At the beginning of my application, i declare a static class of it: static cCountryList CounryList;

    In the constructor i print a message to indicate if the construction was succesful or not. but now when i start my application, i found out that the constructs my class 2 times. and keeps it in the memory. at least... the memory could be reserved because of stack expension? well.. i'm curious... because the construction of my class take something about 5 seconds, so the whole startup scenario takes in 11 seconds. and it waste's something like 15 mb for each instance. I'm using the VC8 Compiler with Native c++.

    Can anyone help? would be really great

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Post a simplified (but complete) example which shows the problem.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Are you doing something like this:
    Code:
    cCountryList CounryList = cCountryList();
    Which **might** cause the cosntructor to be called twice? Again, some code would be nice.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    30
    nope nothing, i think i've found the problem, however its not really logical.. however. i had declared this object in a header witch is included in every other header file. however with #pragma once, it should just load once because of my structure... well, i found a place else to declare it, and now it just constructs once. probally just a bug in my file structure. anyway thanks for the answers

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > however with #pragma once, it should just load once because of my structure
    That means you only get it once per source file.
    If you have two source files, then you'll have two objects.

    Putting source code in header files for this (and many other reasons) is generally a bad idea.
    There's a FAQ on writing multi-file projects.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    30
    thanks for the answer, i'm surely going to look for some good faqs about this. i know loads about programming ( no ego ), but i'm really unknown to this kind of things, working with big projects, and that is what i'm doing right now. anyone knows a good article/FAQ or something else about this subject. it would really help me out, because loads of info written on the internet is written by peaple wo dont know much about the subject. i'd lile to have a fine source, i'm really eager to learn. thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. functions and passing data
    By redmondtab in forum C Programming
    Replies: 41
    Last Post: 09-21-2006, 12:04 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM