class constructer question

This is a discussion on class constructer question within the C++ Programming forums, part of the General Programming Boards category; First what is the difference when using MyClass mc(val); vs using MyClass *mc = new MyClass(val); 2nd question. If I ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53

    class constructer question

    First what is the difference when using

    MyClass mc(val); vs using
    MyClass *mc = new MyClass(val);


    2nd question.

    If I put the declartion of the classes in a loop the "MyClass mc(val)" will cause stack overflow problems where the second doesn't. I have my own thoughts, but wanted to see if I van get more info. Thank you to who respond.

    Shawn

  2. #2
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Hmm.. I don't know about stack overflow, but the second one allocates new memory for the class, and should be freed at the end of the program, or else a memory leak has been born.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  3. #3
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53
    Another question, Will the two diffrent declarations have an effect on Constructer use? Will one way allow for diffrent things. For example if I had several types of Constricters for that class whould they be effected by the type of declartion.

  4. #4
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    To answer that question, you need to give us (me) some examples of situations in which you use it, and we may bew able to tell you if that will allow you to use it in different wats.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  5. #5
    Registered User
    Join Date
    Aug 2001
    Location
    Fort Worth, TX
    Posts
    53
    What I am wanting is how the Constructer is used with the two diffrent declartions. Does the Constructer react diffrent when used in the contex of MyClass mc(val) vs when the Constructer is used in the contex of MyClass * mc = new MyClass(val); Just looking for information on constructer use? Does this give more information to answer my question. Thank you.

  6. #6
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    Oh, IC. No, the constructor won't react differently. The only thing that is different, is that the first won't specifically aallocate any memory, but the second wil. AND, THIS MEMORY MUST BE FREED.

    Code:
    char *c = new char[10000];
    delete [] c; // Do this or else huge memory leak
    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  2. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. question about DLL's and class functions
    By btq in forum Windows Programming
    Replies: 2
    Last Post: 02-25-2003, 05:08 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21