Thread: Purpose of Dynamic Classes?

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

    Purpose of Dynamic Classes?

    Can you someone please expalin to me the purpose of dynamic classes? I know HOW to do it, but I don't know WHY to do it. Arrays I understand, as you usually need the size to be determined at runtime. However, why classes? If any variables inside need to be dynamic, I'll declare them as such. I just can't seem to grasp it...

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    What exactly do you mean by "Dynamic" Classes ?

    As you "know how to do it", posting a small example would help.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    30
    A class... that's dynamic.

    Code:
    class foo{};
    foo bar = new foo();

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by Shokwav View Post
    A class... that's dynamic.

    Code:
    class foo{};
    foo bar = new foo();
    You have to use the new keyword in order to create an instance of the object,call the constructor and allocate memory!By saying "Dynamic" Classes you might to be a little confused.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Unfortunately, the class is not 'dynamic' by any interpretation of the term 'dynamic'.
    You are confusing a class and an object of that class.

    Btw, new returns a pointer to a foo object .
    So it'd be like:
    Code:
    foo* bar = new foo();
    It is mainly useful because the object is created on the heap and is not lost like local variables at the end of scope.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    30
    Quote Originally Posted by manasij7479 View Post
    Unfortunately, the class is not 'dynamic' by any interpretation of the term 'dynamic'.
    You are confusing a class and an object of that class.
    Yeah, I should have said dynamic objects, sorry.

    Quote Originally Posted by manasij7479 View Post
    Btw, new returns a pointer to a foo object .
    So it'd be like:
    Code:
    foo* bar = new foo();
    Derp, I forgot the *...

    Quote Originally Posted by manasij7479 View Post
    It is mainly useful because the object is created on the heap and is not lost like local variables at the end of scope.
    This makes a lot of sense. I was wondering if there was any other deeper purpose, but this makes a lot of sense; thanks.

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Shokwav View Post
    I was wondering if there was any other deeper purpose
    There are a few, which you'll learn as soon as the issues start biting you. (Object Splicing in inheritance is the only one I can recall now.)

    And of course the heap is many hundred times larger than the stacks.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by std10093 View Post
    You have to use the new keyword in order to create an instance of the object,call the constructor and allocate memory!By saying "Dynamic" Classes you might to be a little confused.
    Errr, they can be declared on the stack, too, and it will work just fine.

    Btw, another use is when you have different parts of the program that needs to share the same object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to classes with dynamic memory, looking for some help
    By Freddy92 in forum C++ Programming
    Replies: 4
    Last Post: 03-08-2011, 02:45 AM
  2. Writing classes of dynamic size
    By sjweinberg in forum C++ Programming
    Replies: 12
    Last Post: 08-28-2009, 09:28 PM
  3. Dynamic allocation of 2 dim. arrays in classes
    By circuitbreaker in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2008, 12:13 PM
  4. Classes, dynamic variables, and leaks.
    By Grins2Pain in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2003, 03:07 PM
  5. Purpose of Abstract Classes
    By luckygold6 in forum C++ Programming
    Replies: 15
    Last Post: 04-29-2003, 06:24 PM