|
Here is what i am trying to do: create kind of a "supervisor class". Lets say, we got a class named "Parent", and, a class named "Child". Before initializing a "Child", we need to init "Parent" first. Parent has a boolean variable, which is going to be set to TRUE while initializing Parent class. Parent also has a function and a structure, which (function) should collect integers (into a structure), passed from Child class and then do some manipulations on these ints. So first, we initialize Parent, which sets boolean var to true. Next we initialize Child, which checks if Parent's boolean var is true. If it is, Child calls Parent's "collect" function and passes some random ints to it.. So my first approach was a creation of nested classes (here is some offtopic example):
class xxx
{
protected:
int x;
int y;
private:
public:
int ddd;
class ccc
{
protected:
private:
public:
int sad()
{
printf("%d\r\n", ddd);
}
ccc(){}
~ccc(){}
};
xxx()
{
ddd = 0;
x = 10;
y = 11;
}
~xxx()
{
x = 0;
y = 0;
}
};
In ccc::sad i cannot access var ddd from class xxx. Okay, so i have made it static. Now compiler is not pleased at all:
error LNK2001: unresolved external symbol "public: static int xxx::ddd" (?ddd@xxx@@2HA)
Alright, trying something different (this example is pretty much close to that, what i am about to achieve) :
namespace Some
{
class q
{
public:
typedef struct _H
{
int l[];
}
H;
H *h;
int e;
BOOL g;
int inc_e(int i)
{
e++;
h->l[e] = i;
printf("i = %d\r\n", i);
return 1;
}
int o()
{
printf("%d\r\n", e); return e;
}
q(){ e = 0; g = TRUE; }
~q(){ e = 0; g = FALSE; }
};
class w : q
{
public:
int i;
int inc()
{
if(g)
{
i++;
inc_e(i);
}
return 0;
}
int some()
{
printf("%d\r\n", e); return 0;
}
w(){ i = 0; }
~w(){}
};
}
But that is all wrong again and i dont know again
How to create such relation between two classes, so one can watch over another? I have tried some other ways, some simple things, like:
declare a variable in 1 class, then inherit another class from 1 class, then increment that val in child class. For example we got
int a = 1; in Parent class.
Incrementing this same "a" in a child, which inherits from parent (a++) class and then print out - fine, it increments in child class.
But it stays the same in Parent class (a = 1).
Thanks
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
To declare a variable as static, you must define the variable in 1 and only 1 source file.
class xxx {
public:
static int ddd;
};
int xxx::ddd = 0;
This solution only allows a single instance of the class xxx .
To be able to access a variable from another class instance the class pointer is required. You can push this into the child class when it is created.
class xxx {
protected:
int x;
int y;
private:
public:
int ddd;
class ccc {
protected:
private:
xxx *m_pParent;
public:
int sad() {
printf("%d\r\n", m_pParent->ddd);
}
ccc(xxx *pParent) : m_pParent(pParent) {}
~ccc(){}
};
xxx() {
ddd = 0;
x = 10;
y = 11;
}
~xxx() {
x = 0;
y = 0;
}
};
An even better solution would be to declare class ccc a friend of class xxx so so you can make the variable int ddd protected or private.
|
|
|
|
|
Thanks Andrew, you helped me out once again.
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
While your inquiry is already marked as solved, I wonder if you are really doing the right thing.
First of all, why use a nested class? I've never ever seen a problem that would require such a construction. For the purpose of reusability and information hiding, defining these two classes seperately would always be the better solution. If there are circular dependencies you can get around them with forward declarations.
Second, if you want to access a variable from another class, the best way is to write an accessor for it, rather than making the variable public.
Third, what you described last, i. e. inheriting your Child class form the Parent class, was wrong for at least two reasons: first, an instance of the Child class is not a Parent, and that would be what inheritance would imply. Second, the Parent instance would become part of every child instance, so you'd have an individual Parent object for each child, and each would only have this particular child that it is part of - that is why you always got a==1 .
Fourth, one of your ideas was to use static variables. But if you do that, you can only ever have one parent object. If that is what you want, and you are sure you will never ever need several different parents, each with their own children, then you can make Parent a singleton class. You could then solve your problem like that:
class Parent {
private:
int sum;
Parent() : sum(0) {}
Parent(const Parent&) {}
Parent& operator=(const Parent&) {}
public:
static Parent* get();
void work(const class Child& child);
int sum() const { return sum; }
};
class Child {
private:
int x;
public:
Child(int a);
int value() const { return x; }
};
#include "Parent.h"
#include "Child.h"
static Parent* theParent = 0;
Parent* Parent::get() {
if (theParent==0)
theParent = new Parent;
return theParent;
}
Parent::work(const Child& child) {
sum += child.value();
}
#include "Child.h"
#include "Parent.h"
Child::Child(int a) : x(a) {
Parent* parent = Parent::get();
parent->work(*this);
}
#include "Child.h"
#include "Parent.h"
void test() {
Child abe(3), bert(4), chris(5);
printf("sum=%d\r\n", Parent::get()->sum()};
}
This implementation may also use the Child class without ever referring to the Parent class, and vice versa, i. e. you could produce children in one module and query the Parent in another, and you'd still get correct results.
[edit]Added missing implementation for work[/edit]
modified on Monday, February 7, 2011 8:55 AM
|
|
|
|
|
Hi all,
i m using EditBox with multiline option.
i want to use text formating option like Bold,Italic,Underline etc,please tell me how can i use it in EditBox
if possible please explain me with example.
please help me for this.
thanks in advance.
|
|
|
|
|
you need to use a rich edit control, which is an extension to the edit control. This is the control that you get in wordpad.
If you are using MFC there is a class CRichEdit[^].
This can mostly be used like a standard edit control, however yo set or get the text you need to use streaming functions otherwise the markup will show as plain text.
see CRichEditCtrl::StreamIn[^] for setting the text and CRichEditCtrl::StreamOut[^] for reading text with its formatting tags
An alternate would be a html control. There are quite a few articles on codeproject with samples.
|
|
|
|
|
C language program.
Project launched three stepper motor, uln2803, parallel port.
|
|
|
|
|
|
|
Hi
I need help...
I have an array of string private variable List<String^>^ CalculateGreeks_; having property for get and set List<String^>^ CalculateGreeks() in a class CArgsRequestCredential,
I want to put these variables
PV,Delta,Vega
in my local object
<b>ReqCredObj_ = gcnew Args::CArgsRequestCredential();</b>
first i need to split strings saving each string in a variable and then storing variables in the list..
my code is
<b>List<String^>^ CalculateGreeks1 = gcnew List<String^>();
for(int i = 0 ; i <= LocalArray->GetUpperBound(0);i++)
{
//
//CalculateGreeks1->Add(ii);
String^ Test = Convert::ToString(LocalArray->GetValue(i,0));
array <String ^>^ strings = Test->Split(',');
//ii = gcnew List<String^>^ CalculateGreeks_();
//ii->CalculateGreeks = Convert::ToString(strings);
// CalculateGreeks1->Add(strings);
// ReqCredObj_->CalculateGreeks = Convert::ToString(LocalArray->GetValue(0,1));
}
</b>
but my logic is not giving correct values
kindly help
|
|
|
|
|
This has already been posted to the Managed forum.
I must get a clever new signature for 2011.
|
|
|
|
|
around 100,000 items are added to a CListCtrl.
before adding, I call ShowWindow(SW_HIDE), this function reduces nearly 1/6 time used.
LockWindowUpdate() and ValidateRect() do not reduce time at all.
Do you have idea to add huge items to CListCtrl faster?
|
|
|
|
|
1. Call SetRedraw(FALSE) before adding
2. For best possible speed use virtual listctrl
|
|
|
|
|
Great answer.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|
|
SetRedraw(FALSE) is no better than ShowWindow(SW_HIDE), and there is no double effects when call both - time reduced is same as by calling ShowWindow(SW_HIDE).
I think there should be other ways for faster adding items.
|
|
|
|
|
includeh10 wrote: I think there should be other ways for faster adding items.
Yes, there is a much, much faster way. The secret? Don't add them at all. Instead, use a virtual listctrl to supply the items "on demand".
Anytime you try to add > 1000 items you will see slowdown. Adding 100,000 items will be incredibly annoying to your users.
|
|
|
|
|
It depends of course on the goal of your program, but 100,000 items seems an excessive amount; the most notable speed improvement would come from not adding so many items. If this were possible, it would also speed things up later when you're scrolling the view, selecting items, deleting selections etc.
Maybe it would be useful to add some filter functionality, this would both help the loading time and help the user find what he wants more easily.
modified 13-Sep-18 21:01pm.
|
|
|
|
|
|
Hi All,
If a process creats a threads and then detaches that thread. Now If I kill my process then in this case will my thread run upto its completion ?
|
|
|
|
|
pandit84 wrote: If a process creats a threads and then detaches that thread.
I have no idea what this means.
pandit84 wrote: Now If I kill my process then in this case will my thread run upto its completion ?
If it is a native application, then every other thread created within the application will be killed abruptly when the main thread comes to a halt. If you want a graceful exit, you need to take care of that yourself.
For example:
UINT AdditionalThread(LPVOID p)
{
return 0;
}
int main()
{
_beginthread(AdditionalThread, 0, 0);
return 0;
}
"Real men drive manual transmission" - Rajesh.
|
|
|
|
|
Rajesh R Subramanian wrote: If it is a native application, then every other thread created within the application will be killed abruptly when the main thread comes to a halt.
Technically this actually isn't the case, although from C/C++ it generally is (unless you take unusual measures). See here[^] for a more complete explaination.
Steve
|
|
|
|
|
Stephen Hewitt wrote: See here[^] for a more complete explaination.
I love it. One of the posts on that link says "Win32 is a component of the .NET system". Dang! I've been programming .NET all this time! So there, Nish.
|
|
|
|
|
There is an open secret in Redmond that .NET has build-in time travel, if you don't believe me, what do you think the "T" on .NET stands for?
|
|
|
|
|
Threads can not be detatched from the containing process. A process provides the context the threads runs in (such as the address space) and it doesn't really even make sense to consider a thread without one.
Steve
|
|
|
|
|
As others have correctly pointed out, user mode threads always runs in the context of its process and cannot be detached.
However you can create a kernel mode thread using PsCreateSystemThread[^] which does not run in any process context.
But you can only call this routine from driver code.
|
|
|
|
|