C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-17-2009, 12:42 AM   #31
Registered User
 
Join Date: Jan 2008
Posts: 562
you mentioned above about constraints, where in the code that you suggested is the constraint that would enforce it to take an int as an argument?

the += is just an operator overloading to add two vectors
-EquinoX- is offline   Reply With Quote
Old 11-17-2009, 12:47 AM   #32
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,314
Quote:
Originally Posted by -EquinoX-
you mentioned above about constraints, where in the code that you suggested is the constraint that would enforce it to take an int as an argument?
The partial example that I gave does not make use of that. What it does make use of is a copy assignment operator, which I assume is among "the full set of operators provided below". If it is not... then your best option is to use a std::vector instead of an array of N elements (and if you are not allowed to use std::vector, then you should use placement new, and everything just becomes more complicated).

Quote:
Originally Posted by -EquinoX-
the += is just an operator overloading to add two vectors
Sure, but what does "add two vectors" mean? You have to be clear on that, since it could mean the pairwise addition of the elements of the two vectors, or it could mean the concatenation of one vector to the other, or something else.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-17-2009, 12:57 AM   #33
Registered User
 
Join Date: Jan 2008
Posts: 562
I guess it would add each element that is the vector with each element on the other vector, if the size doesn't match it would throw an exception..
-EquinoX- is offline   Reply With Quote
Old 11-17-2009, 12:59 AM   #34
Registered User
 
Join Date: Jan 2008
Posts: 562
here's the continuation of the instruction, if you wish to know the full set of operators provided below are:

Your template class should provide at least the following class or global operator methods:
1 =, ==, !=, +, +=, -, -=, [ ]. Both binary, and unary, operator- should be provided.
2 In operations with scalar type values, the operators *, /, *=, /= can be used.

which raises up a new question, how can the element type also have the following operators...

Last edited by -EquinoX-; 11-17-2009 at 01:10 AM.
-EquinoX- is offline   Reply With Quote
Old 11-17-2009, 01:50 AM   #35
Registered User
 
C_ntua's Avatar
 
Join Date: Jun 2008
Posts: 1,275
Don't you want something like

Code:
vector& operator*=(const vector& other)
{
     if (this->size != other.size)
         throw std::length_error("No matching size");
     for(int i=0; i < this->size; ++i)
         this->elements[i] *= other.elements[i];
    return *this;
}
or
Code:
vector& operator*=(const vector& other)
{
     for(int i=0; i < this->size && i < other.size; ++i)
         this->elements[i] *= other.elements[i];
     return *this;
}

Last edited by C_ntua; 11-17-2009 at 01:52 AM.
C_ntua is offline   Reply With Quote
Old 11-17-2009, 08:36 AM   #36
Registered User
 
Join Date: Jan 2008
Posts: 562
but it only says for operation with scalar values... so how can I know if it's scalar or not, is it directly saying that if I am storing a vector of char or string then I can't use this
-EquinoX- is offline   Reply With Quote
Old 11-17-2009, 06:10 PM   #37
Registered User
 
Join Date: Jan 2008
Posts: 562
anyone care to give me some insights?
-EquinoX- is offline   Reply With Quote
Old 11-17-2009, 08:45 PM   #38
3735928559
 
Join Date: Mar 2008
Posts: 693
Code:
template<typename T,size_t N>foo
{
  private:
    T  arr[N];
  public:
    foo& operator*=(T t)
    {
       for(size_t i=0;i<N;++i)
       {
          arr[i]*=t;
       }
       return *this;
    }
};
m37h0d is offline   Reply With Quote
Old 11-17-2009, 10:14 PM   #39
Registered User
 
Join Date: Jan 2008
Posts: 562
what if what's stored in i can't be multiplied by t? what if T is a char or a string?

also after giving it some thought, I think type is a class, as the vector is storing an object of type... therefore type is another class... I just don't know how can I make so that type can implement the same operator that vector has, such as *=, etc and I don't know the purpose of type having an int as an argument... if type is a string, then the string takes an int as an argument... but what is that int for in a string? any ideas?

Last edited by -EquinoX-; 11-17-2009 at 10:18 PM.
-EquinoX- is offline   Reply With Quote
Old 11-17-2009, 11:15 PM   #40
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,314
Quote:
Originally Posted by -EquinoX-
so how can I know if it's scalar or not, is it directly saying that if I am storing a vector of char or string then I can't use this
A char is a scalar.

Quote:
Originally Posted by -EquinoX-
what if what's stored in i can't be multiplied by t? what if T is a char or a string?
Then that is the user's problem (though in the case of char or any integer/floating point type, overflow is also a problem, but at run time). In some later version of C++ there may be the concept of concepts, where it can be clearly enforced that a template can only be instantiated with a type that supports certain operations, but for now (and perhaps even in C++0x) such a feature does not exist, though Boost.ConceptCheck makes an attempt at it.

Quote:
Originally Posted by -EquinoX-
also after giving it some thought, I think type is a class, as the vector is storing an object of type... therefore type is another class... I just don't know how can I make so that type can implement the same operator that vector has, such as *=, etc and I don't know the purpose of type having an int as an argument... if type is a string, then the string takes an int as an argument... but what is that int for in a string? any ideas?
It is the other way round: you tell your users: my template works with types that support these operations with these semantics. If you instantiate it with a type that does not support these operations or with the wrong semantics, good luck.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-18-2009, 06:37 AM   #41
3735928559
 
Join Date: Mar 2008
Posts: 693
look at std::valarray.
m37h0d is offline   Reply With Quote
Old 11-18-2009, 08:57 AM   #42
Registered User
 
Join Date: Jan 2008
Posts: 562
I think the only way I can restraint it is for the template class vector to take an object type... type can be anything but then I can guarantee that type has all the overload operators
-EquinoX- is offline   Reply With Quote
Old 11-18-2009, 09:21 AM   #43
3735928559
 
Join Date: Mar 2008
Posts: 693
no you can't. you're just kicking the can down the road.

the point of making that restriction is that your vector class isn't supposed to be used with any type!

if you make a Type<std::string> you're going to get a compilation error in Type just the same as you with vector<std::string> if you ever use *= operator of the class.

you're still looking at the phrasing of the assignment backwards:

Quote:
2 In operations with scalar type values, the operators *, /, *=, /= can be used.
you're looking at this as a requirement of your class to provide a way to support those operators no matter what stupid thing gets specified as a template parameter type.

what that really means is that is the operators that can be used with your vector - it's a requirement of the parameter types, not your vector! you must provide them and use the parameter type's operator, but you are not supposed to try to support an operator that doesn't exist.

besides, you'll see that eventually, if you ever intend to use those operators, the underlying paramter type must have them implemented anyway, so your strategy just adds a layer of complication with no gain over the simpler implementation i am suggesting to you.

Last edited by m37h0d; 11-18-2009 at 09:29 AM.
m37h0d is offline   Reply With Quote
Old 11-18-2009, 09:31 AM   #44
3735928559
 
Join Date: Mar 2008
Posts: 693
Code:
#include <valarray>



int _tmain(int argc, _TCHAR* argv[])
{
  std::valarray<std::string>thisMakesNoSense;
  thisMakesNoSense*="asdf";
	return 0;
}
m37h0d is offline   Reply With Quote
Old 11-18-2009, 11:46 AM   #45
Registered User
 
Join Date: Jan 2008
Posts: 562
well it's a requirement for both the vector and the type parameter... I am only confused on how can I make so that whatever type is the parameters then it would support those operators
-EquinoX- is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an object of a class inside another class - possible? Programmer_P C++ Programming 34 08-15-2009 11:21 PM
Need help with a dynamically allocated object inside another class arya6000 C++ Programming 2 11-28-2008 05:15 PM
Unresolved external on vector inside class constructor Mario F. C++ Programming 13 06-20-2006 12:44 PM
Declaring an instance of a class inside a class nickodonnell C++ Programming 4 10-01-2005 11:46 PM
how do i define a const inside a class under private Unregistered C++ Programming 3 11-01-2001 06:01 PM


All times are GMT -6. The time now is 09:23 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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