Re: Static methods and members
Don't top-post, please. I've rearranged your reply.
Vincent RICHOMME wrote:
Victor Bazarov a 9crit :
Vincent RICHOMME wrote:
First my questions are related to C++ even if I am talking about
managed c++(.NET)...
I am currently implementing some interesting .NET classes in
c++(native code) and I am not an expert with static methods.
Here is what I am doing (class to manage processes) :
.h
namespace System{
namespace Diagnostics{
class ProcessStartInfo
{
[...]
};
class Process
{
[...]
static ProcessStartInfo m_ProcessStartInfo;
};
} //Diagnostics
} //System
//-------------------------------------------------------------------
.cpp
#include "SystemDiagnosticsProcess.h"
#include "Tlhelp32.h"
namespace System{
namespace Diagnostics{
[...]
Add here:
ProcessStartInfo Process::m_ProcessStartInfo;
That's called "the definition" for your static data member.
} // Diagnostics
} // System
//-----------------------------------------------------------------
When I compile I get :
unresolved external symbol "protected: static class
System::Diagnostics::ProcessStartInfo
System::Diagnostics::Process::m_ProcessStartInfo"
(?m_ProcessStartInfo@Process@Diagnostics@System@@1VProcessStartInfo@23@A
How should I fix this.
See above.
Another question is, in managed c++ you can do :
[...]
Ask in a newsgroup that deals with mananged C++. I only know of
'microsoft.public.vc.language', there are probably others.
V
Thanks for the answer about the definition but I have another
question : how con I declare a templated class called Array and
taking pointer on object.
template< arguments > class Array; // that's a declaration. Make sure
// the arguments are the ones you want
I repeat just as an example in managed c++ (I don't want to go on a
newsgroup for .NET) you do this
array<Process^>^localAll = Process::GetProcesses();
I have no idea what it means.
How should I translate this into standard C++
How should I know? It's quite possible that it can't be translated.
Could the follwing work :
vector<Process*> processes = Process::GetProcesses();
and inside my Process::GetProcesses() I would do something like :
vector<Process*> Process::GetProcesses()
{
BOOL bOk = FALSE;
vector<Process*> processes;
while(bOk)
{
bOk = GetNexProcess(&sProcessInfoFromOS);
processes.push_back( new Process( sProcessInfoFromOS ) );
}
return processes;
}
I don't know. Does it do what you want? What is it you want, actually?
You create a vector of pointers to 'Process' objects, and return it from
that function. Seems OK to me.
How can I rename vector<Process*> into Array<Process*>? Maybe I should
inheritate from vector but don't know how to do this, I could try this
template <typename Ptr>
class Array : public vector<Ptr>
{
};
Does it work for you? I often find the need to use non-default vector
constructors, so an empty class simply deriving from 'vector' won't
work because constructors are not inherited.
And I would like that when my array is destroyed all my pointers get
deallocated.
You would need to do it yourself. And I would recommend explicitly
naming your class to hint that it's an array of pointers:
template<typename T> class AutoPtrArray : vector<T*>
{
public:
~AutoPtrArray() {
for (size_t i = 0; i < size(); ++i)
delete at(i);
}
};
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask