473,407 Members | 2,326 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,407 software developers and data experts.

how to pin_ptr a whole object?

when i doing this:

pin_ptr<A> pA = gcnew A; // assume A is a ref class

i'll get many compiler errors.
does anyone know how to do it correctly?
Thanks in advance.

Jan 7 '06 #1
11 2686
well, for one thing, your constructor call does not have round braces to it.
it should be A().

also, A pinning pointer can point to a reference handle, value type or boxed
type handle, member of a managed type or to an element of a managed array.
It cannot point to a reference type.

this compiles:
Splash ^ sp = gcnew Splash();
pin_ptr<int> pI = &sp->myInt;

while this doesn't:
pin_ptr<Splash> pS = gcnew Splash();

kind regards,
Bruno.
<Ta*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
when i doing this:

pin_ptr<A> pA = gcnew A; // assume A is a ref class

i'll get many compiler errors.
does anyone know how to do it correctly?
Thanks in advance.

Jan 7 '06 #2
In addition to Bruno's explanations, you should also know that having a
pinning pointer refering into your object actually pins the whole object.

"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:eS****************@TK2MSFTNGP11.phx.gbl...
well, for one thing, your constructor call does not have round braces to
it. it should be A().

also, A pinning pointer can point to a reference handle, value type or
boxed type handle, member of a managed type or to an element of a managed
array. It cannot point to a reference type.

this compiles:
Splash ^ sp = gcnew Splash();
pin_ptr<int> pI = &sp->myInt;

while this doesn't:
pin_ptr<Splash> pS = gcnew Splash();

kind regards,
Bruno.
<Ta*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
when i doing this:

pin_ptr<A> pA = gcnew A; // assume A is a ref class

i'll get many compiler errors.
does anyone know how to do it correctly?
Thanks in advance.


Jan 7 '06 #3
"Bruno van Dooren" <br**********************@hotmail.com> wrote
well, for one thing, your constructor call does not have round braces to
it. it should be A().

Why? The new-initializer is and has always been optional in
new expressions. This is no different in C++/CLI or even for
gcnew expressions.

In C++ omitting the parens is IMHO the preferred way
as these can lead to confusions with function types.

-hg
Jan 7 '06 #4
but that only works if is there is an appropriate default constructor.
else you will get compiler error C2512: no appropriate default constructor.

using round braces will work in all situations, and it also shows that you
explicitly want to use a specific constructor.

kind regards,
Bruno.


"Holger Grund" <ho**********@remove.ix-n.net> wrote in message
news:OS**************@TK2MSFTNGP11.phx.gbl...
"Bruno van Dooren" <br**********************@hotmail.com> wrote
well, for one thing, your constructor call does not have round braces to
it. it should be A().

Why? The new-initializer is and has always been optional in
new expressions. This is no different in C++/CLI or even for
gcnew expressions.

In C++ omitting the parens is IMHO the preferred way
as these can lead to confusions with function types.

-hg

Jan 7 '06 #5
Bruno van Dooren wrote:
but that only works if is there is an appropriate default constructor.
else you will get compiler error C2512: no appropriate default constructor.

using round braces will work in all situations, and it also shows that you
explicitly want to use a specific constructor.


Are you sure? If there's no default constructor, gcnew T(); will fail
the exact same way as gcnew T;. In C++ we never had to use the () syntax
to call the default constructor. It's C# and Java syntax.

On the contrary, I'm pretty sure it's a mistake to use the () syntax to
call the default constructor. The compiler will treat you code as a
function declaration:

class C
{
public:
C();
};

void Test()
{
C test();
// this is not the default constructor, but a function declaration!
}

With new and gcnew you are allowed to use () with the default
constructor, but I feel it's a bad practice to do so. It's definitely
not an error to skip the () with the default constructor.

Tom
Jan 9 '06 #6
i think it is a matter of taste really. i am used to using round braces
because to me, it means call constructor.

always using round braces works if there is a default constructor, and if
there isn't.
whereas not using them will work with default constructors, but not with
other constructors.

that's why i prefer always to use braces. but it's not an error to either do
it or not do it.

kind regards,
BRuno.
"Tamas Demjen" <td*****@yahoo.com> wrote in message
news:uv**************@TK2MSFTNGP10.phx.gbl...
Bruno van Dooren wrote:
but that only works if is there is an appropriate default constructor.
else you will get compiler error C2512: no appropriate default
constructor.

using round braces will work in all situations, and it also shows that
you
explicitly want to use a specific constructor.


Are you sure? If there's no default constructor, gcnew T(); will fail the
exact same way as gcnew T;. In C++ we never had to use the () syntax to
call the default constructor. It's C# and Java syntax.

On the contrary, I'm pretty sure it's a mistake to use the () syntax to
call the default constructor. The compiler will treat you code as a
function declaration:

class C
{
public:
C();
};

void Test()
{
C test();
// this is not the default constructor, but a function declaration!
}

With new and gcnew you are allowed to use () with the default constructor,
but I feel it's a bad practice to do so. It's definitely not an error to
skip the () with the default constructor.

Tom

Jan 9 '06 #7
Bruno van Dooren wrote:
always using round braces works if there is a default constructor, and if
there isn't.
whereas not using them will work with default constructors, but not with
other constructors.


That logic makes sense. It's really a style issue, a matter of personal
preference.

With the stack syntax, however, it's an error to use the extra ():

MyClass class();

This doesn't create a new instance called "class", but rather declares a
function named "class" that returns MyClass and takes no argument.
That's why many C++ programmers don't like the idea of using the ()
syntax with default constructors. But with "new" or "gcnew", it's really
optional.

Tom
Jan 9 '06 #8
Tamas Demjen wrote:
That logic makes sense. It's really a style issue, a matter of personal
preference.


That's not entirely true. You're all correct that putting empty parenthesis
after the type in local declaration can be confused with a function
prototype, the new expression is very different.

In general, if the () are included in a new expression (i.e. "new T()"),
then the item is value intialized (basically the compiler zeros out the
memory first). If the parenthesis are omitted, and the type is a non-POD,
item is default initialized. If the item is POD type, then state of the
program is indeterminate (and in some cases the program is ill-formed).

So, there's a performance cost to including the () in a new expression, but
it makes the program more likely to be correct.

More info is in the C++ Standard 5.3.4/15.

For gcnew, the CLR always zeros out memory before any constructor runs... so
the difference between default initialized and value initialized isn't any
different.

--
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
Bugs? Suggestions? Feedback? http://msdn.microsoft.com/productfeedback/
Jan 9 '06 #9
Tau
Ok, that's the answer I exactly want to know.
Thanks, Marcus and Bruno!

ps: as to the parenthesis problem, i really don't whether it will cause
problems.
C++/CLI is some kind hard for me to understand now. :-)

Jan 10 '06 #10
You might also want to take a look at my article :-
http://www.codeproject.com/managedcp...edpointers.asp

--
Regards,
Nish [VC++ MVP]
<Ta*******@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
when i doing this:

pin_ptr<A> pA = gcnew A; // assume A is a ref class

i'll get many compiler errors.
does anyone know how to do it correctly?
Thanks in advance.

Jan 10 '06 #11
Tau
Thanks, Nishant
That really helps! :-)

Jan 11 '06 #12

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

Similar topics

2
by: T Ray Humphrey | last post by:
I could not get the following code to compile: public ref class wrapper { void Read(int^ mpLen) { pin_ptr<int> pLen = mpLen; unmanaged* punman = new unmanaged(); punman->Read(pLen); return; }
1
by: _DS | last post by:
My managed code needs to shuttle an unmanaged buffer between a couple unmanaged functions. I know this could be done via pin_ptr, and I understand the implications re managed heap fragmentation...
3
by: Nobody | last post by:
I posted this bug to MS, but was wondering if someone else could try inserting the following into a C++/cli project and seeing if it causes the compiler to barf: array<float,2>^ fMatrix = gcnew...
7
by: _iycrd | last post by:
Is there an easy way to pin a pointer that is a member of a class, and leave that pointer pinned for the duration of the class's existence? The pointer would presumably be pinned inside the class's...
23
by: digitalorganics | last post by:
How can an object replace itself using its own method? See the following code: class Mixin: def mixin(object, *classes): NewClass = type('Mixin', (object.__class__,) + classes, {}) newobj =...
4
by: adodotnet20 | last post by:
I'm developing a Windows application and I'm having some problems in making variable accessible to the whole application. I use VS.NET 2005 and I create a database connection object in the method ...
1
by: Bruce | last post by:
public ref class GpsDevice abstract { /* Please note GarXface4::GpsDevice* is in a different namespace from this one and GarXface4::GpsDevice is an unmanaged class */ virtual...
7
by: David Lowndes | last post by:
I've got a project with a fair bit of mixed C++/CLI calling native code, often passing raw pointers - and I'm worrying that we may have occasions where we don't use pin_ptr when we need to. ...
3
by: Andy | last post by:
Hello, I have the following situation: Thread A is allocating a dataset, doing some low-level calculations and storing a pointer to the dataset in a std::list via push_back. Thread B should...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.