![]() |
| | #46 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
Quote:
__________________ 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 | |
| | #47 | |
| 3735928559 Join Date: Mar 2008
Posts: 693
| Quote:
Code: #include <valarray>
int _tmain(int argc, _TCHAR* argv[])
{
std::valarray<std::string>thisMakesAtLeastSomeSense;
thisMakesNoSense+="asdf";
return 0;
}
this doesn't. Code: #include <valarray>
int _tmain(int argc, _TCHAR* argv[])
{
std::valarray<std::string>thisMakesNoSense;
thisMakesNoSense*="asdf";
return 0;
}
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 | |
| | #48 | |
| 3735928559 Join Date: Mar 2008
Posts: 693
| Quote:
Last edited by m37h0d; 11-18-2009 at 01:24 PM. | |
| m37h0d is offline | |
| | #49 |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,768
| 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 | |
| | #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 | |
| | #51 | |
| 3735928559 Join Date: Mar 2008
Posts: 693
| Quote:
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 | |
| | #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 | |
| | #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 | |
| | #54 | ||
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
Quote:
__________________ 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 | |
| | #55 |
| Registered User Join Date: Jan 2008
Posts: 562
| does all type takes an int as an argument in the constructor? |
| -EquinoX- is offline | |
| | #56 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 11,372
| Quote:
__________________ 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 | |
| | #57 | |||
| 3735928559 Join Date: Mar 2008
Posts: 693
| Quote:
Quote:
Code: arr[i]=T(i); Quote:
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 | |
| | #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 | |
| | #59 |
| 3735928559 Join Date: Mar 2008
Posts: 693
| that's what i've been trying to tell you! |
| m37h0d is offline | |
| | #60 |
| Registered User Join Date: Jan 2008
Posts: 562
| okay, now define what is an objects of built in type? |
| -EquinoX- is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |