| View previous topic :: View next topic |
| Author |
Message |
ajay.sonawane@gmail.com Guest
|
Posted: Fri Jun 10, 2005 4:30 pm Post subject: Class template and static member function |
|
|
How to call static member function of template class.
For example
template <typename T>
class A
{
private:
int m_i;
static m_j;
public:
int GetSum()
{
return m_i + T::i;
}
static int GetJ()
{
return m_j;
}
}
class B
{
public:
int i;
}
I can use this template class as
A<B> m_A;
m_A.GetSum();
But how could I access static member function GetJ() of template class
A ?
Is it like this
A<>.GetJ();
Let me know solution.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Sat Jun 11, 2005 10:47 am Post subject: Re: Class template and static member function |
|
|
[email]ajay.sonawane (AT) gmail (DOT) com[/email] wrote:
| Quote: | How to call static member function of template class.
For example
template <typename T
class A
{
private:
int m_i;
static m_j;
public:
int GetSum()
{
return m_i + T::i;
}
static int GetJ()
{
return m_j;
}
}
class B
{
public:
int i;
}
I can use this template class as
A
m_A.GetSum();
But how could I access static member function GetJ() of template class
A ?
Is it like this
A<>.GetJ();
|
The qualified name of a static member function of a template class is
formed in exactly the same way as it is with any other class: simply
take the name of the class, add a double colon, then add the name of
the static member fuction.
So if the template class is A<B>, then it follows that
A<B>::GetJ()
is the name of its GetJ() static member function.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Mon Jun 13, 2005 7:50 pm Post subject: Re: Class template and static member function |
|
|
-- Greg wrote:
| Quote: | So if the template class is A<B>, then it follows that
A<B>::GetJ()
is the name of its GetJ() static member function.
|
One could also reference the function via the instance: m_A.GetJ().
Perhaps Ajay was trying to use a static member shared between all
template classes of different types (A<B>, A<C>, etc.). That can't be
done with a static member variable.
M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ajay.sonawane@gmail.com Guest
|
Posted: Tue Jun 14, 2005 10:07 am Post subject: Re: Class template and static member function |
|
|
Hello mlimber
You are right, I am tring to use a staic member shared between all
template classes of different types, Why can't I do so ?
mlimber wrote:
| Quote: | -- Greg wrote:
So if the template class is A<B>, then it follows that
A<B>::GetJ()
is the name of its GetJ() static member function.
One could also reference the function via the instance: m_A.GetJ().
Perhaps Ajay was trying to use a static member shared between all
template classes of different types (A<B>, A<C>, etc.). That can't be
done with a static member variable.
M
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Tue Jun 14, 2005 7:37 pm Post subject: Re: Class template and static member function |
|
|
-- ajay.sonaw... (AT) gmail (DOT) com wrote:
| Quote: | You are right, I am tring to use a staic member shared between all
template classes of different types, Why can't I do so ?
|
It's because of the way templates work. For each specialization of your
template class A for some other class B, the compiler will generate
code for A in terms of B. If you have multiple specializations, you'll
get multiple versions of A generated for each specialization.
Consider the definition that is required for your counter A<>::m_j. You
can make it a template, just like your class:
template <class T> int A<T>::m_j = 0;
Then, when you specialize A as A<B> and A<C>, the compiler will
generate:
int A<B>::m_j = 0;
int A<C>::m_j = 0;
(You could also make your own specializations:
template<> int A<B>::m_j = 0;
but that won't give you what you're looking for either.)
In short, you can't do it because static members belong to the class
not the object, and templates are an automated way to generate
*different* classes (A<B>, A<C>, etc.). Each m_j will belong to its
specialized class and shared among all objects of that class, but each
specialized class is distinct from all the other specializations and
thus does not share the static member variables.
There are a variety of other ways to solve this problem, however.
Cheers!
M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gianluca Guest
|
Posted: Wed Jun 15, 2005 9:38 am Post subject: Re: Class template and static member function |
|
|
[note to moderators: i just sent a reply previous to this which I think
it's wrong. Please ignore it]
{Such requests are often cannot be met because different moderators
handle the two posts. Please follow instructions provided by following
the link at the bottom of every posted article. -mod}
You can do it like so:
class SharedAmongTemplates
{
public:
static int m_j;
static int GetJ()
{
return m_j;
}
};
template <typename T>
class A : public SharedAmongTemplates
{
private:
int m_i;
public:
int GetSum()
{
return m_i + T::i;
}
};
HTH
Gianluca
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Wed Jun 15, 2005 10:22 am Post subject: Re: Class template and static member function |
|
|
In article <1118387983.822927.105270 (AT) g44g2000cwa (DOT) googlegroups.com>,
<ajay.sonawane (AT) gmail (DOT) com> wrote:
| Quote: | How to call static member function of template class.
For example
template <typename T
class A
{
private:
int m_i;
static m_j;
public:
int GetSum()
{
return m_i + T::i;
}
static int GetJ()
{
return m_j;
}
}
class B
{
public:
int i;
}
I can use this template class as
A
m_A.GetSum();
But how could I access static member function GetJ() of template class
A ?
Is it like this
A<>.GetJ();
|
class A_base
{
static int m_j;
public:
static int GetJ() {return m_j;}
};
template <class T>
class A:private A_base
{
int m_i;
public:
using A_base::GetJ;
};
int A_base::m_j = 10;
void test()
{
A<int> c;
int x = c.GetJ();
}
is one approach if you truly want the same static member for each
instance of A.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ajay.sonawane@gmail.com Guest
|
Posted: Wed Jun 15, 2005 10:26 am Post subject: Re: Class template and static member function |
|
|
Excellent explanation...
Now its pretty much clear why I can't do so.
" There are a variety of other ways to solve this problem, however. "
But I am eager to know other ways to solve this problem.
Awaiting for your reply.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Wed Jun 15, 2005 10:42 pm Post subject: Re: Class template and static member function |
|
|
In article <1118722850.219660.112200 (AT) f14g2000cwb (DOT) googlegroups.com>,
[email]ajay.sonawane (AT) gmail (DOT) com[/email] writes
| Quote: | Hello mlimber
You are right, I am tring to use a staic member shared between all
template classes of different types, Why can't I do so ?
Because a class template is a mechanism for generating class |
definitions, the result classes have nothing whatsoever to do with each
other.
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gleb Alexeev Guest
|
Posted: Wed Jun 15, 2005 10:43 pm Post subject: Re: Class template and static member function |
|
|
| Quote: | You are right, I am tring to use a staic member shared between all
template classes of different types, Why can't I do so ?
You can derive your template class from non-template base class, which |
will hold your static data member.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ajay.sonawane@gmail.com Guest
|
Posted: Thu Jun 16, 2005 8:17 am Post subject: Re: Class template and static member function |
|
|
Thats fine, But Deriving template class from other class will solve
the problem ???
How will I call the static method ? I will have to use template class
in order to call static method ?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
mlimber Guest
|
Posted: Fri Jun 17, 2005 10:21 am Post subject: Re: Class template and static member function |
|
|
-- ajay.sonaw... (AT) gmail (DOT) com wrote:
| Quote: | Thats fine, But Deriving template class from other class will solve
the problem ??? How will I call the static method ? I will have to use
template class in order to call static method ?
|
See Carl Barron's post above. You could also invoke the static method
without an instance:
int j = A<int>::GetJ();
int k = A<float>::GetJ();
in which case j and k will be identical.
Cheers!
M
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Sun Jun 19, 2005 4:41 pm Post subject: Re: Class template and static member function |
|
|
mlimber wrote:
| Quote: | -- ajay.sonaw... (AT) gmail (DOT) com wrote:
Thats fine, But Deriving template class from other class will solve
the problem ??? How will I call the static method ? I will have to use
template class in order to call static method ?
See Carl Barron's post above. You could also invoke the static method
without an instance:
int j = A<int>::GetJ();
int k = A<float>::GetJ();
in which case j and k will be identical.
|
Or you could ignore the template class altogether and just invoke the
static function in the base class:
struct Base
{
static int GetJ();
};
template <class T>
struct A : public Base
{
using Base::GetJ;
...
};
Now calling Base::GetJ() invokes the same function as A<int>::GetJ() or
A<char>::GetJ() or any other type that A may be instantiated with.
Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|