Pages: « 1 2 3 4 5 6 »
  Print  
Author Topic: Happy New Year  (Read 10874 times)
Offline (Male) Josh @ Dreamland
Reply #45 Posted on: January 15, 2010, 06:15:42 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
Of course, but now it adds an additional pointer instead of just being in a nice array. Which means the struct itself can likewise be in a nice array, no pointers. Less chance for segfault that way, too.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) Rusky
Reply #46 Posted on: January 16, 2010, 11:28:33 AM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
If you were score_under you could just set up the stack yourself and allocate dynamically-sized stuff on the stack. :P
Logged
Offline (Male) RetroX
Reply #47 Posted on: January 16, 2010, 12:04:47 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
In C++ you can make statically-allocated containers of varying sizes:
Code: [Select]
template <typename Value, int size>
class Stack {
private:
    Value value[size];
public:
    ...
};

or create constant values for classes:
Code: [Select]
template<const int id> class thing
{
public: const int get_id() { return id; }
}
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Josh @ Dreamland
Reply #48 Posted on: January 16, 2010, 06:38:04 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2949

View Profile Email
What's with the const? Template parameters take the pickiest form of const you can even imagine. That's like prefixing "long" to a Bignum.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Male) RetroX
Reply #49 Posted on: January 17, 2010, 10:39:57 AM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
What's with the const? Template parameters take the pickiest form of const you can even imagine. That's like prefixing "long" to a Bignum.
Code: [Select]
template<int id> class thing
{
public: int get_id() { return id; }
}
« Last Edit: January 17, 2010, 11:44:09 AM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #50 Posted on: January 18, 2010, 03:27:45 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
...why do you need templates for that?
Logged
Offline (Male) RetroX
Reply #51 Posted on: January 18, 2010, 07:40:35 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
...why do you need templates for that?
So that you can have a constant value in a certain class as a type.  That class alone is useless, but you can store other things in it.  That meaning, you can actually find out what class is what and perform an action based upon this depending on what the class is.
« Last Edit: January 18, 2010, 07:48:19 PM by RetroX » Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Male) Rusky
Reply #52 Posted on: January 19, 2010, 10:36:42 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
So basically manual RTTI and/or virtual functions?
Logged
Offline (Male) RetroX
Reply #53 Posted on: January 20, 2010, 03:23:32 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
So basically manual RTTI and/or virtual functions?
RTTI, yes, virtual functions... I guess, but not really.

Bad example, but:
Code: [Select]
template<bool negative> class number
 {
 public: uint64 value;
 public: bool is_negative() { return negative; }
 }
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #54 Posted on: January 20, 2010, 05:51:12 PM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Code: [Select]
class Array{
public: virtual size_t getLength() = 0;
};

class StaticArray<int length> : public Array{
public:
void* arrayData[T];
virtual size_t getLength(){
 return length;
}
}

I didn't bother to check if this compiles, but if this is possible, then it's a more efficient solution for static arrays with a getLength property than the ones I used in the past.
Logged
Offline (Male) RetroX
Reply #55 Posted on: January 20, 2010, 06:55:52 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
Code: [Select]
class Array{
public: virtual size_t getLength() = 0;
};

class StaticArray<int length> : public Array{
public:
void* arrayData[T];
virtual size_t getLength(){
 return length;
}
}

I didn't bother to check if this compiles, but if this is possible, then it's a more efficient solution for static arrays with a getLength property than the ones I used in the past.
Better example than mine.  Although, another thing that I would add is changing void to a class T in the template.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #56 Posted on: January 21, 2010, 10:04:09 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
I thought about that too, but I didn't remember the syntax for type templates in C++.

Code: [Select]
template <typename T>
class Array{
public:
virtual int getLength() = 0;
};

template <typename T, int size>
class StaticArray : public Array<T>
{
public:
T data[size];
virtual int getLength(){ return size; }
};

int main(){
StaticArray<int, 10> array;
return 0;
}
Logged
Offline (Male) RetroX
Reply #57 Posted on: January 24, 2010, 09:24:52 PM

Master of all things Linux
Contributor
Location: US
Joined: Apr 2008
Posts: 1055
MSN Messenger - classixretrox@gmail.com
View Profile Email
I thought about that too, but I didn't remember the syntax for type templates in C++.

Code: [Select]
template <typename T>
class Array{
public:
virtual int getLength() = 0;
};

template <typename T, int size>
class StaticArray : public Array<T>
{
public:
T data[size];
virtual int getLength(){ return size; }
};

int main(){
StaticArray<int, 10> array;
return 0;
}
What exactly is the point of the first class?  It doesn't do anything, and you don't really need it.
Logged
My Box: Phenom II 3.4GHz X4 | ASUS ATI RadeonHD 5770, 1GB GDDR5 RAM | 1x4GB DDR3 SRAM | Arch Linux, x86_64 (Cube) / Windows 7 x64 (Blob)
Quote from: Fede-lasse
Why do all the pro-Microsoft people have troll avatars? :(
Offline (Unknown gender) luiscubal
Reply #58 Posted on: January 25, 2010, 09:16:55 AM
Member
Joined: Jun 2009
Posts: 452

View Profile Email
Well, you could have this

Code: [Select]
int sum(Array<int>* numbers){
   int result = 0;
   for(int i = 0; i < numbers->getLength();++i){
       result += numbers[i];
   }
   return result;
}

int main(){
    StaticArray<int, 10> array1;
    StaticArray<int, 20> array2;
    printf("%d\n", sum(&array1) + sum(&array2));
    return 0;
}
Logged
Offline (Male) Rusky
Reply #59 Posted on: January 25, 2010, 08:54:07 PM

Resident Troll
Joined: Feb 2008
Posts: 955
MSN Messenger - rpjohnst@gmail.com
View Profile WWW Email
Basically, it links the different classes the template creates so they can behave as one class (which they pretty much are). Notice that the non-type parameters mean you have to generate a different version of the code for every size... isn't it more efficient space-wise (and time-wise, considering the virtual function) to just make the number a constructor parameter?
Logged
Pages: « 1 2 3 4 5 6 »
  Print