Purpose of Dynamic Classes?

This is a discussion on Purpose of Dynamic Classes? within the C++ Programming forums, part of the General Programming Boards category; Can you someone please expalin to me the purpose of dynamic classes? I know HOW to do it, but I ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    What exactly do you mean by "Dynamic" Classes ?

    As you "know how to do it", posting a small example would help.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,783
    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, 01: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, 11:13 AM
  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

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