Thread: How to instantiate classes on cue.

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912

    How to instantiate classes on cue.

    I'm creating a program that creates family trees. I've figured an equation thatcan calculate exactly how many records to create based on how many generations (assuming you have two parents to the person, R=2E+G-1 OR R=2^G-1). Anyway is there anyway I can have a function that will create and name, based on a string passed to it (increment a number with in it for each instance). How would I do that? Also - what file do I use to use files in straight C?

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    use token pasting.

    [code]
    #include <iostream.h>//I am too lazy to change
    #define CONCAT(a,b) a##b//this a##b is pasting the tokens together

    int main()
    {
    int CONCAT("variable",1)=7
    cout<<variable1;

    return 0;
    }

    This program will output 7



    /*EDIT: To find out more about this, look for the C++ Book Using C++. It has an entire chapter on all preprocessor directives.*/
    Last edited by golfinguy4; 02-04-2002 at 08:16 PM.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    sorry about question 2. Here's my advice:

    Go to the C board.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    for your information

    I'm a little shaky on your question; but, I do know how to maintain a variable beyond the lifetime of a function. Its quite simple actually.

    All you have to do is include the keyword 'static' before a member variable in a struct/class or before a top-level variable within a function. In effect what this does is give the variable global lifetime but limited scope. If you create the variable in a function, then you must remember to initialize it. This is the value it starts with when the program loads up and you don't have to worry about it being reset on every function call. If you define it in a class, you must remember to include the following after defining the class: data_type class_name::mem_var = init_val;

  5. #5
    Not sure what it is you want incremented?

    Use an array.
    string myStr [val]; //This creates an array with the number of entries that the val variable holds which is presumably the result from your Equation.

    Use a for() loop to create your arrays, and in the loop use the 3rd arg to hold the increment var.

    for(int i = 0, i < 100, i++)

    Then, just pass that increment var value (i++) to another variable outside the scope of the for loop or with global scope.

    Arrays don't need to have numbers afixed to them, they already operate that way - starting from 0 to 1 number less than what you originally specified the size to be.

    string myStr[9];

    myStr[0] myStr[1] myStr[2] ...... myStr[7] myStr[8] //Stops at 8, 1 less than nine.

    If this doesn't solve your problem you need to be more clear about what you want.
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 10:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM