473,378 Members | 1,417 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Static methods and members

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@@1 VProcessStartInfo@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.


May 17 '06 #1
5 2648
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@@1 VProcessStartInfo@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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #2
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 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@@1 VProcessStartInfo@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

May 17 '06 #3
On Wed, 17 May 2006 21:21:52 +0200, Vincent RICHOMME wrote:
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<> adding?
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.
May 17 '06 #4
Don't top-post, please. I've rearranged your reply.

Vincent RICHOMME wrote:
Victor Bazarov a écrit :
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@@1 VProcessStartInfo@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
May 17 '06 #5
Andre Kostur wrote:
On Wed, 17 May 2006 21:21:52 +0200, Vincent RICHOMME wrote:
[...]
template <typename Ptr>
class Array : public vector<Ptr>
{
};
No. std::vector is not intended to be inherited from.


Why the hell not? Who started that rumour anyway?
[...]


V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Neil Zanella | last post by:
Hello, I would like to know whether it is possible to define static class methods and data members in Python (similar to the way it can be done in C++ or Java). These do not seem to be mentioned...
3
by: DanielBradley | last post by:
Hello all, I have recently been porting code from Linux to cygwin and came across a problem with static const class members (discussed below). I am seeking to determine whether I am programming...
3
by: Mauzi | last post by:
hi, this may sound odd and noob like, but what is the 'big' difference between static and non-static funcitons ? is there any performace differnce? what is the best way to use them ? thnx ...
3
by: Jay | last post by:
Why are there static methods in C#. In C++ static was applied to data only (I believe) and it meant that the static piece of data was not a part of the object but only a part of the class (one...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
9
by: Clint | last post by:
Hey all - Excuse the cross-post ... I'm not sure what the appropriate newsgroup would be for this question. I have a question that I'm not quite sure how to ask. For all I know, I have the...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
9
by: sonu | last post by:
Hi All, Pls clarify me what is the difference between static member and static method in c. pls some one reply me with Example.
2
by: Vivek Ragunathan | last post by:
Hi Are the members in a static class in C# class synchronized for multiple thread access. If yes, are all static members in a C# class auto synchronized ? Regards Vivek Ragunathan
13
by: learning | last post by:
Hi I have a static class written by other team which encapsulates a database instance. but I need to extend it to incldue other things. I know that C# static class is sealed and can;t be inherited...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.