Thread: Implement an N deminsional mathematical vector.

  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587

    Implement an N deminsional mathematical vector.

    How could I make a constructor that accepts a templated number of floats?

    Code:
    template<int N>
    class vector
    {
         vector(float _1, ..., float _N); // ???
    };

  2. #2
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I'm using C++0x, btw,

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    To be honest, this sounds more like a job for ... then for templates.

  4. #4
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Is it possible to limit the number of arguments with ...?

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by User Name: View Post
    Is it possible to limit the number of arguments with ...?
    There's probably an internal limit of something outrageous like 16384, but I don't know of a way to limit it otherwise. (Flip side: same would be true of your template solution if it existed.)

  6. #6
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    I realize there is a limit to the size of the stack.

    How would you define an N dimensional vector?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Really depends on the context of what I'm using these for. Since you wrote "mathematical vector" I'm assuming you're looking for something you want to be able to do operations like + and * on. Since you can't ever really operate with two different sizes at the same time, I don't know that I'd worry about making them compatible unless you have to make a bunch of different sizes.

  8. #8
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    That's a good idea. I was planning on only allowing N vectors to be used with N vectors, it was just the generalization of the math that I was after. I'll still use the template, but leave it absract and specialize the ones I actually plan to use.

    Like so:
    Code:
    template<int N>
    class vector
    {
    	public:
    	vector(...) = 0;
    };
    
    template<>
    class vector<3>
    {
    	public:
    	vector(float, float, float)
    	{
    	}
    	
    	vector<3> operator+(const vector<3>&){}
    };
    Thanks for your time and help.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For taking a variable argument list, you might use std::initializer_list if it's supported.
    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. Mathematical Operation
    By madahmad1 in forum C Programming
    Replies: 3
    Last Post: 08-18-2008, 10:46 AM
  2. Replies: 9
    Last Post: 11-12-2007, 03:29 PM
  3. Mathematical problem
    By Warlax in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2006, 02:38 AM
  4. Need mathematical help
    By Magos in forum C++ Programming
    Replies: 10
    Last Post: 03-24-2002, 10:42 AM
  5. mathematical calculations
    By Unregistered in forum Windows Programming
    Replies: 7
    Last Post: 01-10-2002, 11:24 PM