Static methods and members

V

Vincent RICHOMME

Hi,


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
#include <vector>
using namespace std;


namespace System{
namespace Diagnostics{


class ProcessStartInfo
{
public:
friend class Process;

ProcessStartInfo();

protected:
PROCESS_INFORMATION m_sProcessinfo;
};


class Process
{
public:

Process(void);
~Process(void);

public:
//static vector<Process> GetProcesses();
static void Start(LPCTSTR tszName);
//void Start(ProcessStartInfo& procinfo);
CString GetProcessName();
DWORD GetId();

protected:
static ProcessStartInfo m_ProcessStartInfo;
};



} //Diagnostics
} //System

//-------------------------------------------------------------------
..cpp
#include "SystemDiagnosticsProcess.h"
#include "Tlhelp32.h"


namespace System{
namespace Diagnostics{

ProcessStartInfo::processStartInfo()
{
memset(&m_sProcessinfo, 0, sizeof(m_sProcessinfo) );
}

Process::process(void){}
Process::~Process(void){}



/*static*/
void Process::Start(LPCTSTR tszName)
{

BOOL bRet = CreateProcess(tszName,
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
NULL,
&(m_ProcessStartInfo.m_sProcessinfo) );

}

} // 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.

Another question is, in managed c++ you can do :

array<Process^>^localAll = Process::GetProcesses();

I would like to do the same, I suppose(not sure)that I should declare in
my class a vector<Process*> and rename it as Array<Process>.
I tried to rename it via a typdef but it doesn't work and in addition if
I create a System::Test::MyClass with a method returning a
vector<MyClass*> I will have to do it again.

How can I declare a global class(seen in all my namespaces) called Array
and that takes pointer on object.
 
V

Victor Bazarov

Vincent said:
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
 
V

Vincent RICHOMME

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.

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();

How should I translate this into standard C++

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;
}


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>
{
};

And I would like that when my array is destroyed all my pointers get
deallocated.







Victor Bazarov a écrit :
Vincent said:
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
 
A

Andre Kostur

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.

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();

No clue what that means. (Nor am I specifically interested at this time).
How should I translate this into standard C++

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;
}

Seems functional enough.
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

Why would you want to? What additional functionality is Array said:
template <typename Ptr>
class Array : public vector<Ptr>
{
};

No. std::vector is not intended to be inherited from.
And I would like that when my array is destroyed all my pointers get
deallocated.

Don't store pointers into the vector. Store smart pointers (such as
boost::shared_ptr or std::tr1::shared_ptr) instead.

Thus you may have:

vector<std::tr1::shared_ptr<Process> > processes = Process::GetProcesses();

And make the appropriate changes in your GetProcesses() call. As a
result, when the members of the vector are destroyed, they will delete the
pointer that they hold.
 
V

Victor Bazarov

Don't top-post, please. I've rearranged your reply.

Vincent said:
Victor Bazarov a écrit :
Vincent said:
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.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top