C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 11-18-2009, 12:13 PM   #46
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by -EquinoX-
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
If you really want to make sure that vector's element type can support all the operators, then the solution is to create a vector class and an element class. Note, classes, not class templates. Once you involve templates, it becomes possible to try to instantiate the template with a type that does not support the operations required.

Quote:
Originally Posted by -EquinoX-
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
Assume that the type supports those operators and thus make use of them. The compiler will then enforce this requirement for you by spitting out an error if the user attempts to call a function that assumes that a particular operator is available when it is not.
__________________
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, 01:10 PM   #47
3735928559
 
Join Date: Mar 2008
Posts: 693
Quote:
Originally Posted by -EquinoX- View Post
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
this compiles
Code:
#include <valarray>

int _tmain(int argc, _TCHAR* argv[])
{
  std::valarray<std::string>thisMakesAtLeastSomeSense;
  thisMakesNoSense+="asdf";
	return 0;
}
because string has a += operator

this doesn't.
Code:
#include <valarray>

int _tmain(int argc, _TCHAR* argv[])
{
  std::valarray<std::string>thisMakesNoSense;
  thisMakesNoSense*="asdf";
	return 0;
}
because string doesn't have a *= operator.

what you still don't realize is that even if you create a template class that has the operator in question, you can't guarantee you can use it.

you're transferring the inevitable compilation error from your vector class to your element class.
m37h0d is offline   Reply With Quote
Old 11-18-2009, 01:20 PM   #48
3735928559
 
Join Date: Mar 2008
Posts: 693
Quote:
Originally Posted by laserlight View Post
If you really want to make sure that vector's element type can support all the operators, then the solution is to create a vector class and an element class. Note, classes, not class templates. Once you involve templates, it becomes possible to try to instantiate the template with a type that does not support the operations required.
if he doesn't use templates, then he can't actually use the operators at all can he?

Last edited by m37h0d; 11-18-2009 at 01:24 PM.
m37h0d is offline   Reply With Quote
Old 11-18-2009, 02:56 PM   #49
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,768
Quote:
Originally Posted by m37h0d View Post
Code:
I don't really think that the implementation must be inside the class
d'oh of course. that is correct. you just can't put it in a separate file.
Sure you can, although it wouldn't be normal. The definition just has to be available somehow, that's all.
__________________
"Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot
brewbuck is offline   Reply With Quote
Old 11-18-2009, 03:01 PM   #50
Registered User
 
Join Date: Jan 2008
Posts: 562
okay, I am now 100% sure after consulting here and there that I don't need to create an element class.. all I need is just one template vector class.. only two problem left..

1. how can I make sure that the element (which will be stored inside the vector) supports all of
this operators:

=, ==, !=, +, +=, -, -=

2. any idea why the element takes an int? I've been thinking all day and can't seem the reason
-EquinoX- is offline   Reply With Quote
Old 11-18-2009, 03:13 PM   #51
3735928559
 
Join Date: Mar 2008
Posts: 693
Quote:
Originally Posted by -EquinoX- View Post
okay, I am now 100% sure after consulting here and there that I don't need to create an element class.. all I need is just one template vector class.. only two problem left..

1. how can I make sure that the element (which will be stored inside the vector) supports all of
this operators:

=, ==, !=, +, +=, -, -=

2. any idea why the element takes an int? I've been thinking all day and can't seem the reason
i already showed you #1.

re: #2, only thing that occurred to me was looping over the array of T's and initializing each one with its index in the array.
m37h0d is offline   Reply With Quote
Old 11-18-2009, 03:29 PM   #52
Registered User
 
Join Date: Jan 2008
Posts: 562
for #1, you only showed me that some types makes sense to use *= and some don't, but it doesn't really tell me that the element supports all of the operator, correct me if I am understanding you wrong
-EquinoX- is offline   Reply With Quote
Old 11-18-2009, 09:38 PM   #53
3735928559
 
Join Date: Mar 2008
Posts: 693
the compiler will tell you as soon as someone tries to use an operator that doesn't exist.
m37h0d is offline   Reply With Quote
Old 11-18-2009, 10:03 PM   #54
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by m37h0d
if he doesn't use templates, then he can't actually use the operators at all can he?
Of course he can, since he explicitly provides them, i.e., creating one's own number class.

Quote:
Originally Posted by m37h0d
re: #2, only thing that occurred to me was looping over the array of T's and initializing each one with its index in the array.
Yes, though strictly speaking that is assignment, not initialisation.
__________________
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, 10:06 PM   #55
Registered User
 
Join Date: Jan 2008
Posts: 562
does all type takes an int as an argument in the constructor?
-EquinoX- is offline   Reply With Quote
Old 11-18-2009, 10:14 PM   #56
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,372
Quote:
Originally Posted by -EquinoX-
does all type takes an int as an argument in the constructor?
All objects of built-in types can be initialised with an int or have an int assigned to them, but they do not actually have constructors. For class types, it depends on the class type. As I noted, some class types are deliberately designed as numeric types and thus have similiar behaviour in many respects, e.g., the std::complex class template.
__________________
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, 10:21 PM   #57
3735928559
 
Join Date: Mar 2008
Posts: 693
Quote:
Originally Posted by laserlight View Post
Of course he can, since he explicitly provides them, i.e., creating one's own number class.
aah ok, i see what you mean. no abstraction or genericity at all.

Quote:
Yes, though strictly speaking that is assignment, not initialisation.
you're correct, i phrased myself poorly. i was thinking of the initialization on the right hand side...i.e.:
Code:
arr[i]=T(i);

Quote:
does all type takes an int as an argument in the constructor?
not every class does, but if in your constructor, you assign each element in your array to an instance that has been constructed with one, then any type put into your vector must.

e.g:
Code:
template<typename T,size_t N>foo
{
  private:
    T  arr[N];
  public:
    foo()
    {
       for(size_t i=0;i<N;++i)
       {
          arr[i]=T(i);
       }
    }
};

Last edited by m37h0d; 11-18-2009 at 10:31 PM.
m37h0d is offline   Reply With Quote
Old 11-19-2009, 10:05 AM   #58
Registered User
 
Join Date: Jan 2008
Posts: 562
so I came to a conclusion that there is no way to make sure that type has all the operators:

=, ==, !=, +, +=, -, -=

and every objects of built in type can take an int as a constructor
-EquinoX- is offline   Reply With Quote
Old 11-19-2009, 10:25 AM   #59
3735928559
 
Join Date: Mar 2008
Posts: 693
that's what i've been trying to tell you!
m37h0d is offline   Reply With Quote
Old 11-19-2009, 03:14 PM   #60
Registered User
 
Join Date: Jan 2008
Posts: 562
okay, now define what is an objects of built in type?
-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 06:11 AM.


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