d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Questions > Yo!
Add Reply New Topic New Poll
Member
Posts: 2,441
Joined: Sep 6 2008
Gold: 3,505.00
Jan 17 2015 04:30pm
Hey guys.

I had an exam monday at my university in c++. I had the program i had to write and the main.cpp said my mark is 3 but then the professor just asked questions i had not a single idea about whats the right answer so i have to go again the next monday. fml :<

So there was a static npos stuff i had to use after making my own template class to override some functions of the std::vector. I managed to do it but i do not really understand why this npos stuff have to be static, also i dont get what punctually typename and typedef means which i used too to solve the problems.. (yep im kinda noob and i just used some stuff from the internet, but yep the professor fucked me up with those questions ).
Im sure its not that hard, but the education of some courses is really akward at us, like this one, so you fuck up 30-40 hours in a semester and literally wont understand a single shit about your own codes.

So if someone could expalin these stuff without reference to stackoverload where every second word is chineese to me, i would be really glad.

Thx! (knock me in da face with a big shark for bad english)
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 17 2015 04:57pm
Typically you make something static when there is a state you want to maintain at the class level, as opposed to at object level. Although, without more information I am not sure what npos is. When you say npos, I think of string::npos. Which is a constant used to indicate the value of a failed search. I have seen it described as the "non-position", "no position", or "null position." Typically when you do a string::find() and a match is not found, the return would be the value set to string::npos, which by default is -1, indicating failure.

In relation to a vector, I am not sure what functions you were overriding. And if you were adding the concept of a npos to this derivation of the vector class, then it would be a static constant, because the value is not meant to change per instance. npos will always be the same regardless of how many instances of vector you instantiate, so its best to just have 1 static copy of it, instead of 1 per object.
Member
Posts: 2,441
Joined: Sep 6 2008
Gold: 3,505.00
Jan 17 2015 05:57pm
Quote (Minkomonster @ 17 Jan 2015 23:57)
Typically you make something static when there is a state you want to maintain at the class level, as opposed to at object level. Although, without more information I am not sure what npos is. When you say npos, I think of string::npos. Which is a constant used to indicate the value of a failed search. I have seen it described as the "non-position", "no position", or "null position." Typically when you do a string::find() and a match is not found, the return would be the value set to string::npos, which by default is -1, indicating failure.

In relation to a vector, I am not sure what functions you were overriding. And if you were adding the concept of a npos to this derivation of the vector class, then it would be a static constant, because the value is not meant to change per instance. npos will always be the same regardless of how many instances of vector you instantiate, so its best to just have 1 static copy of it, instead of 1 per object.


oh thx!
itsnt it a problem that if i make it static for the reason just to have only 1 copy of it, that i can only use it in static functions and objects? i only had the knowledge that static = class level object but it wasnt enogh, prof said its bullshit without more info...
I had to override exactly the find, with the stl::search to search a part of my given vector in the already filled one, so i had to use const_iterals too so if i found the stuff i returned with the iteral, if not then with the npos, not like i understood what i am actually doing, i just knew i have to find a normal stl algorithm and use it, in my template class.

This post was edited by Wikings2 on Jan 17 2015 05:58pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Jan 17 2015 06:13pm
Quote (Wikings2 @ Jan 17 2015 06:57pm)
oh thx!
itsnt it a problem that if i make it static for the reason just to have only 1 copy of it, that i can only use it in static functions and objects? i only had the knowledge that static = class level object but it wasnt enogh, prof said its bullshit without more info...
I had to override exactly the find, with the stl::search to search a part of my given vector in the already filled one, so i had to use const_iterals too so if i found the stuff i returned with the iteral, if not then with the npos, not like i understood what i am actually doing, i just knew i have to find a normal stl algorithm and use it, in my template class.


No, this is not correct. You cannot use a static function or variable in a non-static context. This is not the same as saying it has to be used only in static functions.


Code
class MyClass
{
public:
void MyNonStaticFunction() { }
static void MyStaticFunction() { }
};

class MyOtherClass
{
public:
//non-static function referencing a static function
void MyOtherNonStaticFunction() { MyClass::MyStaticFunction(); }
//static function referencing a non-static function
static void MyOtherStaticFunction() { MyClass mc; mc.MyNonStaticFunction(); }
};

int main()
{
MyOtherClass moc;
moc.MyOtherNonStaticFunction();

MyOtherClass::MyOtherStaticFunction();
}
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll