|
Nandu_77b wrote: Can you please give me some proves.
You mean like this[^]
If I were you I would report to my ISP that Google doesn't work. I would not put up with paying for internet access that doesn't include the most powerful internet tool today.
|
|
|
|
|
Hi All,
From the above discussion we can conclude that INHERETANCE is for both code reuse and specialization as well.
If any one don’t agree with this please ..raise the concerns.
Thanks,
Nandu.
|
|
|
|
|
Hello, i need to creat an array or matrix of short (16 bit) from matrix of integer (32 bit) where all the members of the int matrix are not bigger then short, i mean we will not have a problem. the problem is that i need to do it fast because i have a real time software and i dont want to just loop and copy from all members of array...
could anyone help?
|
|
|
|
|
Hi,
1.
you can't convert a matrix the way you want without looking at all the values.
2.
an array of shorts is smaller than an array of ints (it takes half the amount
of memory or disk space, needs half the time to transmit, etc); it is not
necessarily faster though, since all computations normally use int anyway.
3.
more contextual information might result in better answers.
What is it you want to do ?
How big if the array ? (and how many are there)
What is real-time ? How much time are you willing to spend ?
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
Why are you writing real-time software in C++/CLI?
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
I created a Windows Forms Application, put a TextBox and a Button on it. When I press the Button its event handler calls a function in another CPP module in my project that does the whole bunch of work. As it is doing that work I want it to send some progress text messages to the TextBox on the Form. How can I do this? This was a simple task before as I just used pointers, but with Managed C++ I just can't figure it out. Can somebody help, please?
|
|
|
|
|
Take a look at GetFunctionPointerForDelegate() in MSDN. You define a delegate in managed code, get a function pointer for it, cast it as function pointer for the native function, and then hand it off to the native code to use as a callback into your managed code.
|
|
|
|
|
Michael, thanks a lot for your reply. I did try delegates even before my desperate call for help and now I tried it again, but still no luck. Here is what I put on top of my Form1.h:
<br />
delegate void MyFormMethod (void);
typedef void (*PFUNC)(void);
<br />
PFUNC pFunc (void);
Then in the form's constructor I do this:
<br />
public:<br />
Form1(void)<br />
{<br />
InitializeComponent();<br />
<br />
MyFormMethod^ m = gcnew MyFormMethod(this, &Form1::MyMethod);
IntPtr pint = Marshal::GetFunctionPointerForDelegate (m);
PFUNC pFunc = (PFUNC)pint.ToPointer ();
}<br />
So far so good, but if I try to use pFunc () anywhere in the code the program does not link. Even if I use it in the same Form's Button handler I get an error:
error LNK2028: unresolved token (0A00000A) "void (__clrcall*__clrcall pFunc(void))(void)" (?pFunc@@$$FYMP6MXXZXZ) referenced in function "private: void __clrcall My2::Form1::button1_Click(class System::Object ^,class System::EventArgs ^)" (?button1_Click@Form1@My2@@$$FA$AAMXP$AAVObject@System@@P$AAVEventArgs@4@@Z)
There is just no simple example anywhere that I could start from. I saw the console app examples where they create a delegate in the main() function and then use it in that main() function. That works, but when I try to move the same concept into my form... There should be some obvious problem in my understanding of things, could you please point me to it?
Many thanks!
Eugene
|
|
|
|
|
I highly recommend getting Nishant Sivakumar's "C++/CLI In Action" book if you're looking for examples. He covers a lot of confusing stuff with remarkable clarity and brevity.
How are you using pFunc ? It's a pointer for the native code to use. If you want to use a native function pointer, you'd use GetDelegateForFunctionPointer() in managed code.
Also, I'm not certain, but I'm concerned about the scope of your variables. When m goes out of the scope of Form1(void) it may be garbage collected.
|
|
|
|
|
Thanks, the book I am working with now is "Pro Visual C++/CLI" by Stephen Fraser, but I will look into the book you recommend too.
I see, I guess I am trying to use pFunc() in the managed code itself. But you know, it actually works if I call pFunc() right at the end of the Form constructor. I get a warning message box that it is unsafe to call function pointers from managed code, but when I click "Continue" the pFunc() is actually called, it does its work and returns properly. However, if I call pFunc() from the Button handler of the Form or from elsewhere the project does not even build:
public:
Form1(void)
{
InitializeComponent();
MyFormMethod^ m = gcnew MyFormMethod(this, &Form1::MyMethod);
IntPtr pint = Marshal::GetFunctionPointerForDelegate (m);
PFUNC pFunc = (PFUNC)pint.ToPointer ();
<code>pFunc();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
<code>pFunc();
}
It's a good point about m garbage collected at some point. Although it should be OK since I only needed it at the time I extracted pointer out of it. Then the pointer is stored in pFunc, which is global and should not get deallocated.
By the way, should we be even talking about calls between managed/unmanaged code here? My test app in my example is just a plain WinFormsApp "Hello World" example (should be all "managed") with a few mods: I put function foo() in my WinFormsApp.CPP with the code to access something on the form (in Form1.h), say a textBox . I press the button to call foo() and foo() writes some text, that's it. I cannot use Form1::textBox::Text = "Text" in foo() , I tried to create a new public method in my Form that does this->textBox->Text = "Text" , but the compiler says "cannot access a non-static member". OK, I make my method static but then I cannot access the textBox from it anymore: error C2597: illegal reference to non-static member 'System::Windows::Forms::Form::Text' .
Of course, I will keep reading and searching for the example that does exactly what I am trying to do, but the solution to my problem should be very simple. Maybe it is so simple that they do not explain it in the books. If someone could just tell me what I am missing...
Thanks!
Eugene
|
|
|
|
|
I'm sorry. I took "This was a simple task before as I just used pointers" to mean that you had existing native C++ code calling the C++ module that does all the work and were moving that existing code into C++/CLI.
In your case, you can define an event to update the text:
delegate void UpdateText( String^ );
ref class SomeClass
{
public:
event UpdateText^ OnUpdateText;
};
public ref class Form1 : Form
{
public:
Form1()
{
InitializeComponent();
someClass_ = gcnew SomeClass();
someClass_->OnUpdateText += gcnew UpdateText( this, &Form1::UpdateTextHandler );
}
private:
SomeClass^ someClass_;
void UpdateTextHandler( String^ text )
{
textBox->Text = text;
}
}; In the implementation of SomeClass , call OnUpdateText( "New text" ) when you want to update the text.
-- modified at 22:59 Monday 13th August, 2007
[Some corrections made to above code.]
|
|
|
|
|
Well, it did not work exactly as was written, but certainly led me to the working code, thank you very much!
However, the solution I finally got seems ugly to me:
<font color=green>
<code>public</code> delegate void UpdateText( String^ );
<font color=green>
<code>public</code> ref class SomeClass
{
public:
event UpdateText^ OnUpdateText;
};
public ref class Form1 : Form
{
public:
Form1()
{
InitializeComponent();
someClass_ = gcnew SomeClass();
someClass_->OnUpdateText += gcnew UpdateText( UpdateTextHandler );
}
private:
SomeClass^ someClass_;
void UpdateTextHandler( String^ text )
{
textBox->Text = text;
}
<font color=green>
System::Void button1_Click (System::Object^ sender, System::EventArgs^ e) {
void foo (String^ Text);
foo ("Text for textBox");
}
};
Now here is MyCode.CPP:
#include "stdafx.h"
#include "Form1.h"
using namespace EventTest;
<font color=green>
<code>public ref class MyClass: public SomeClass
{
public:
void MyClass_UpdateText (String^ Text) {
SomeClass^ EventKeeper = gcnew SomeClass;
EventKeeper->OnUpdateText (Text);
}
};</code>
void foo (String^ Text) {
MyClass^ y = gcnew MyClass();
y->MyClass_UpdateText (Text);
}
Question #1: does it really have to be that twisted-complicated or it is just me being a beginner? To update a property on a Form from outside the Form I need to create and delegate an event, that has to be a member of SomeClass. Then to fire this event we have to create a new MyClass based on SomeClass with the actual event handler code and create an instance of it. And only then we can raise this event. Wow! Did I get it right or there is an extra step here?
Question #2: If SomeClass is declared in front of Form1 then I cannot use the form designer. It says: The class Form1 can be designed, but is not the first class in the file. Visual Studio requires that designers use the first class in the file. Move the class code so that it is the first class in the file and try loading the designer again. But I cannot move it anywhere else because it must go before Form1() constructor as it uses this class, right? Is this something I just have to live with or there is a better way?
Sorry for the lengthy description but as I am getting to the bottom of this issue (it's been 2 days) I'd like other people who search for the same answer can read this thread and see something that actually works. I really appreciate the time spent on helping me, hopefully this will help a few more people!
Thanks again
Eugene
|
|
|
|
|
You are definitely doing too much regarding Question #1. You don't need to create an object every time you need to fire an event. I'm sorry my example code wasn't better. I just typed it into a text editor. I should have grabbed it from a working project.
Regarding Question #2, there's definitely a better way. I just stuck everything into one place for simplicity. I'd only confuse you more if I tried to describe a better way, so I'll make a working example in C++/CLI and send that to you.
-- modified at 22:39 Monday 13th August, 2007
To be a little clearer and more correct, here's the form class with irrelevant stuff stripped out:
#include "WorkerClass.h"
namespace WinApp1
{
public ref class Form1 : public Form
{
public:
Form1()
{
InitializeComponent();
workerClass_ = gcnew WorkerClass();
workerClass_->OnUpdateText += gcnew UpdateText( this, &Form1::UpdateTextHandler );
}
private:
TextBox^ textBox_;
Button^ button_;
WorkerClass^ workerClass_;
void button__Click( System::Object^ sender, System::EventArgs^ e )
{
workerClass_->DoSomething();
}
void UpdateTextHandler( String^ text )
{
textBox_->Text = text;
}
};
} And, in another file called WorkerClass.h, here's WorkerClass , which was SomeClass in my previous example:
namespace WinApp1
{
delegate void UpdateText( String^ );
ref class WorkerClass
{
public:
event UpdateText^ OnUpdateText;
void DoSomething()
{
++serialNumber_;
OnUpdateText( Convert::ToString( serialNumber_ ) );
}
private:
int serialNumber_;
};
} You don't need to derive your class from WorkerClass . All you need is an event of type UpdateText^ to which Form1 can attach a handler.
|
|
|
|
|
Well, this code actually works. And the designer works too, BTW.
You just saved another soul that almost started to hate the new technology
Thanks!
Eugene
|
|
|
|
|
In a VS2005 SP1 C++/CLI project I have a class declared like:
ref class CallbackProxy
{
}; This class is a private member of another C++/CLI class, AsioDevice . I've turned on code analysis in Visual Studio, and I get this:
warning: CA2213 : Microsoft.Usage : 'AsioLib.AsioDevice' contains field 'callbackProxy_' that is of IDisposable type: AsioLib.CallbackProxy. Change AsioLib.AsioDevice's Dispose method to call Dispose or Close on this field. How did my CallbackProxy class become IDisposable ? None of its members are IDisposable .
|
|
|
|
|
|
Yeah, it does. But that link doesn't really clarify things for me. In my class' ctor, I new a pointer to a native struct . In my class' dtor, I delete said pointer. If what I'm reading is correct, the dtor is not called when the reference to callbackProxy_ goes out of scope; I need to explicitly call delete on it. That just seems totally counterintuitive to using managed code.
But what I'd really like is a auto_ptr<> -like class, which I found here: http://www.codeproject.com/managedcpp/CAutoNativePtr.asp
|
|
|
|
|
Hi Michael,
I don't know how it is in C++, this is what I can tell you about C#:
- structs are value types, they get allocated on the stack (in the stack frame
of the method that defines them) or inside an object that holds them (similar
to an int). You can "create" them with the new keyword, but that merely serves
as a way to pass initial values. No one cares about destroying them, they
disappear when the method returns.
- class objects are reference types; you create (i.e. allocate and initialize) them
with the new keyword (that would be gcnew in C++), and they get collected by the
gc if no live objects are referencing them any more.
- there is no such thing as a destructor, but you can have a class implement
the IDisposable interface by providing a Dispose method and stating the
interface explicitly.
- There is no automatic addition of a Dispose method or IDisposable interface (as
opposed to the article about C++).
Hope this helps.
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
In C++/CLI, both class and struct are reference types. They need to be explicitly modified using the value keyword to get a value type.
My original problem is a design flaw. If I use a smart pointer, I don't need a destructor anymore and the problem goes away.
-- modified at 11:11 Monday 13th August, 2007
Luc Pattyn wrote: there is no such thing as a destructor
Are you sure about that? I made this class:
class Class1
{
public Class1()
{
Console.WriteLine( "Class1 constructed." );
}
~Class1()
{
Console.WriteLine( "Class1 destructed." );
}
}
and I got "Class1 destructed." in my Output window. It was not deterministic, but it was called.
|
|
|
|
|
His post was C# specific, which is quite different from C++/CLI in some ways. In C# there is no destructor. The destructor syntax in C# ( ~Class1() ) is actually a finalizer. In C++/CLI ~Class1() maps to the Dispose() function from IDisposable giving you a form of deterministic destruction (though memory won't be reclaimed immediately you can count on resources being closed). The !Class() syntax maps to the finalizer in C++/CLI.
|
|
|
|
|
Class1 is a C# class. Take up the semantic argument with Microsoft for putting a "Destructors (C# Programming Guide)" page in the MSDN library.
|
|
|
|
|
semantics and documentation arguments aside, did your question get answered?
|
|
|
|
|
The second post in this thread answered the original question. Thanks, Mark.
|
|
|
|
|
oh right, wasn't paying close enough attention. And yes MS semantics are horribly confusing.
In that case, the C# ~Class1() is the finalizer. And by explicitly defining one you ensure that it is called by the garbage collector before the memory for an instance of the class is reclaimed. So although you're printing 'destructed' its actually the garbage collector calling the finalizer before memory is reclaimed. Its not a destructor that runs when the variable goes out of scope as you would get if it was a C++/CLI ref class with ~Class1() defined. And unlike destruction, finalization is nondeterministic and incurs a high performance cost for both allocation and deallocation. So you never want to write one unless your class contains unmanaged resources that won't be cleaned up by the garbage collector. In that case the finalizer will ensure the unmanaged stuff is cleaned up even if someone forgets to call delete or Dispose .
|
|
|
|
|
I don't want to argue, but you're not being specific enough. A C# destructor is more than syntactic sugar. The code within it will be called and then an implicit call is made to Finalize(). I'm not disputing or judging what actually occurs, I'm just pointing out that "C# destructors do not exist" is not a true statement. And I'm certainly not advocating that every C# class define a destructor. I'm glad I made my original mistake in assuming that C++/CLI ctor/dtor behavior is the same as C++ ctor/dtor behavior because it cleared things up after clouding them for awhile.
|
|
|
|