Like Tree2Likes
  • 1 Post By manasij7479
  • 1 Post By MK27

Questions regarding arrays and classes!!(amateur level)

This is a discussion on Questions regarding arrays and classes!!(amateur level) within the C++ Programming forums, part of the General Programming Boards category; hello everyone, in C++ you have the option of not defining the number of elements you want in an array ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Questions regarding arrays and classes!!(amateur level)

    hello everyone,
    in C++ you have the option of not defining the number of elements you want in an array like this .
    Code:
     int a[]={1,2,3,4,5,6,7};
    i want to mimic the arrays using classes, i want to mimic this very functionality for example:
    Code:
    class Array 
    {
    ............
    .........
    
    .....
    };
    
    void main()
    {
    Array a(1,2,3,4,5,6,7);
    }
    MY QUESTION IS THIS IS IT POSSIBLE TO GIVE AN UNDEFINED NUMBER OF ARGUMENTS IN A CONSTRUCTOR SO I CAN MIMIC THE ARRAY STYLE OF INITIALIZATION.


    if you still dont understand the question let me be more thorough .
    Code:
    int a[]={1,2,3,4,5,6};  // this is possible in c++. Remember the   
                                  //  number of the inputs is undefined. It 
                                  //  depends on the user.  
    
    Array a(1,2,3,4,5,6,6,7);// is the same possible can i use no matter  
                                     // how many arguments i want?
    

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Code:
    {1,2,3,4,5,6}
    This is an initializer list.. not an argument list. Implementing it is a C++11 feature.

    You can also have variable argument lists.. like you said.
    cstdarg (stdarg.h) - C++ Reference
    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
    Nov 2011
    Posts
    5
    Quote Originally Posted by manasij7479 View Post
    Code:
    {1,2,3,4,5,6}
    This is an initializer list.. not an argument list. Implementing it is a C++11 feature.

    You can also have variable argument lists.. like you said.
    cstdarg (stdarg.h) - C++ Reference
    Sir i know its an initializer list which is used in arrays.
    Code:
     int a[]={1,2,3,4,5,6};  // intializer list
    Sir what i want to know is this can i intialize a class named Array like this too as im trying to mimic the functionalities of arrays through classes.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    >what i want to know is this can i intialize a class named Array like this too as im trying to mimic the functionalities of arrays through classes.
    Overload the Assignment Operator to take an initializer list as the argument.
    linkstatic likes this.
    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 !



  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Overloading = is probably better style, but if you want to go with variadic args to the constructor:

    Code:
    #include <iostream>
    #include <cstdarg> // for variadic args
    #include <vector>
    
    class example {
    	public:
    		example (int count, ...);
    };
    
    using namespace std;
    
    int main(int argc, const char *argv[]) {
    	example (3, 1, 2, 3);
    	return 0;
    }
    
    example::example (int count, ...) {
    	va_list args;
    	va_start(args, count);
    	for (int i = 0; i < count; i++) {
    		cout << va_arg(args, int) << endl;
    	}
    	va_end(args);
    }
    linkstatic likes this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    5
    that would do it ^_^
    thanx alot

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    I would not recommend MK27's solution. Instead, I suggest you look up std::initializer_list.
    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.

  8. #8
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Elysia View Post
    I would not recommend MK27's solution. Instead, I suggest you look up std::initializer_list.
    btw.. is there a C++(/11) way to make functions take variable args ?
    (Not passing objects of containers having variable no. of elements )
    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 !



  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    2,351
    yes, you could declare a function like so:

    Code:
    template<typename... Args>
    void myFunc(int arg1, Args... args);
    and then call it recursively until all of the arguments were handled - ie added to the array.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    An std::initializer_list is automatically created from an initializer list in C++11.
    So the function would take an object, but there would be no need for you to create it.
    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.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    boston
    Posts
    5
    i dont even see the similarity of the 2. MK27 example is of a single object with a variable amount of arguments.

    the original code
    Code:
    int a[]={1,2,3,4,5,6};  // initialization list


    is an array of objects each taking a single argument

    if you wanted to create an array of a class that you could initialize a number of them using a initializer list it would be totally different.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,538
    The thing is that the size depends upon the number of arguments, one could say. The OP asked if such a thing could be done with ordinary functions (and hence constructors).
    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.

  13. #13
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by Acorn View Post
    i dont even see the similarity of the 2. MK27 example is of a single object with a variable amount of arguments.
    Well.. the first post also contained
    Array a(1,2,3,4,5,6,6,7);// is the same possible can i use no matter // how many arguments i want?
    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 !



  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Elysia View Post
    I would not recommend MK27's solution. Instead, I suggest you look up std::initializer_list.
    I was not aware of that. It looks to have a limitation vargs doesn't have (all args must be of the same type), altho perhaps that does not matter in this case.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. entry level homework help: Passing Arrays into functions.
    By DHart07 in forum C++ Programming
    Replies: 8
    Last Post: 04-21-2010, 09:11 AM
  2. Questions on Classes
    By nosfearatu in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2010, 03:52 AM
  3. A few questions regarding classes
    By Xzyx987X in forum C++ Programming
    Replies: 4
    Last Post: 11-11-2005, 02:42 PM
  4. C questions- basic Level
    By ronenk in forum C Programming
    Replies: 6
    Last Post: 07-11-2004, 05:12 PM
  5. Questions on Classes
    By Weng in forum C++ Programming
    Replies: 2
    Last Post: 11-18-2003, 05:49 AM

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