473,545 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using a pointer to call Constructor from inside a template

Hello

I need some help from anyone that can provide it. Below is a function
inside a template collection class that I'm writing. I have a TYPE *
that points to an allocated memory location, and I'm explicitly trying
to call the TYPE::TYPE() constructor on it by using the pointer. That
doesn't compile, yet if I forget the usage of TYPE and write the line
to use the actual constructor, then it's fine.

See the code below for an example.

BTW, I have code in another function that uses the same mechanism to
call the destructor and that works with no problem.
pObjToDelete->~TYPE(); // call the destructor on the object to be
erased

Am I doing something wrong?? Any clues would be appretiated.

Thanks
Mike

template <class TYPE>
Coll<TYPE& Coll<TYPE>::add Last(const TYPE & anObject)
{
// I'm casting this pointer to the same type as the template
// parameter, but I know that points to a block of memory of
// proper size for TYPE
TYPE * pNewElement = getFreeElementF romBlocks();

// I get a compile error on this line. If the type that I pass in is
'String', then
// the error message is: 'TYPE' : is not a member of 'String'
pNewElement->TYPE::TYPE( anObject ); // call the copy constructor
// this doesn't work either
pNewElement->TYPE( anObject ); // call the copy constructor

// If I replace it with this EXPLICIT call, then everything is fine,
except
// when I try to use this template with another type
pNewElement->String::String ( anObject ); // call the copy
constructor
}

Nov 20 '06 #1
3 2024
* mi************@ ivara.com:
Hello

I need some help from anyone that can provide it. Below is a function
inside a template collection class that I'm writing. I have a TYPE *
that points to an allocated memory location, and I'm explicitly trying
to call the TYPE::TYPE() constructor on it by using the pointer. That
doesn't compile, yet if I forget the usage of TYPE and write the line
to use the actual constructor, then it's fine.

See the code below for an example.

BTW, I have code in another function that uses the same mechanism to
call the destructor and that works with no problem.
pObjToDelete->~TYPE(); // call the destructor on the object to be
erased

Am I doing something wrong?? Any clues would be appretiated.

Thanks
Mike

template <class TYPE>
Coll<TYPE& Coll<TYPE>::add Last(const TYPE & anObject)
{
// I'm casting this pointer to the same type as the template
// parameter, but I know that points to a block of memory of
// proper size for TYPE
TYPE * pNewElement = getFreeElementF romBlocks();

// I get a compile error on this line. If the type that I pass in is
'String', then
// the error message is: 'TYPE' : is not a member of 'String'
pNewElement->TYPE::TYPE( anObject ); // call the copy constructor
// this doesn't work either
pNewElement->TYPE( anObject ); // call the copy constructor

// If I replace it with this EXPLICIT call, then everything is fine,
except
// when I try to use this template with another type
pNewElement->String::String ( anObject ); // call the copy
constructor
}
The short answer is that since you don't know how to this, you're doing
something you're not qualified to do (sort of like an airplane pilot
asking how to start the plane) -- unless it's a learning exercise.

Assuming it's a learning exercise, use placement new.

Otherwise, don't.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 20 '06 #2

Alf P. Steinbach wrote:
* mi************@ ivara.com:
Hello

I need some help from anyone that can provide it. Below is a function
inside a template collection class that I'm writing. I have a TYPE *
that points to an allocated memory location, and I'm explicitly trying
to call the TYPE::TYPE() constructor on it by using the pointer. That
doesn't compile, yet if I forget the usage of TYPE and write the line
to use the actual constructor, then it's fine.

See the code below for an example.

BTW, I have code in another function that uses the same mechanism to
call the destructor and that works with no problem.
pObjToDelete->~TYPE(); // call the destructor on the object to be
erased

Am I doing something wrong?? Any clues would be appretiated.

Thanks
Mike

template <class TYPE>
Coll<TYPE& Coll<TYPE>::add Last(const TYPE & anObject)
{
// I'm casting this pointer to the same type as the template
// parameter, but I know that points to a block of memory of
// proper size for TYPE
TYPE * pNewElement = getFreeElementF romBlocks();

// I get a compile error on this line. If the type that I pass in is
'String', then
// the error message is: 'TYPE' : is not a member of 'String'
pNewElement->TYPE::TYPE( anObject ); // call the copy constructor
// this doesn't work either
pNewElement->TYPE( anObject ); // call the copy constructor

// If I replace it with this EXPLICIT call, then everything is fine,
except
// when I try to use this template with another type
pNewElement->String::String ( anObject ); // call the copy
constructor
}

The short answer is that since you don't know how to this, you're doing
something you're not qualified to do (sort of like an airplane pilot
asking how to start the plane) -- unless it's a learning exercise.

Assuming it's a learning exercise, use placement new.

Otherwise, don't.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Hey Alf

This group has become a lot more snarky in the last 15 years or so,
when I was last on here :-). I think I'm qualified to do this as I've
done it before - I've just forgotten the syntax. Perhaps someone else
can offer an answer that I can actually use!!

Mike

Nov 20 '06 #3

mi************@ ivara.com wrote:
Alf P. Steinbach wrote:
* mi************@ ivara.com:
Hello
>
I need some help from anyone that can provide it. Below is a function
inside a template collection class that I'm writing. I have a TYPE *
that points to an allocated memory location, and I'm explicitly trying
to call the TYPE::TYPE() constructor on it by using the pointer. That
doesn't compile, yet if I forget the usage of TYPE and write the line
to use the actual constructor, then it's fine.
>
See the code below for an example.
>
BTW, I have code in another function that uses the same mechanism to
call the destructor and that works with no problem.
pObjToDelete->~TYPE(); // call the destructor on the object to be
erased
>
Am I doing something wrong?? Any clues would be appretiated.
>
Thanks
Mike
>
template <class TYPE>
Coll<TYPE& Coll<TYPE>::add Last(const TYPE & anObject)
{
// I'm casting this pointer to the same type as the template
// parameter, but I know that points to a block of memory of
// proper size for TYPE
TYPE * pNewElement = getFreeElementF romBlocks();
>
// I get a compile error on this line. If the type that I pass in is
'String', then
// the error message is: 'TYPE' : is not a member of 'String'
pNewElement->TYPE::TYPE( anObject ); // call the copy constructor
// this doesn't work either
pNewElement->TYPE( anObject ); // call the copy constructor
>
// If I replace it with this EXPLICIT call, then everything is fine,
except
// when I try to use this template with another type
pNewElement->String::String ( anObject ); // call the copy
constructor
}
The short answer is that since you don't know how to this, you're doing
something you're not qualified to do (sort of like an airplane pilot
asking how to start the plane) -- unless it's a learning exercise.

Assuming it's a learning exercise, use placement new.

Otherwise, don't.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Hey Alf

This group has become a lot more snarky in the last 15 years or so,
when I was last on here :-). I think I'm qualified to do this as I've
done it before - I've just forgotten the syntax. Perhaps someone else
can offer an answer that I can actually use!!

Mike
..... but thank you....

placement new, did the trick....

Nov 20 '06 #4

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

Similar topics

28
20281
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object...
40
4226
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the number of records. Both these numbers are known
3
1845
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in include files: typedef Context *Context_ptr; typedef ObjOut<Context> Context_out;
4
2123
by: Carsten Spieß | last post by:
Hello all, i have a problem with a template constructor I reduced my code to the following (compiled with gcc 2.7.2) to show my problem: // a base class class Base{}; // two derived classes
1
4482
by: Adam Dziendziel | last post by:
Hi all! I'm writing a luabind/boost::python-like binding utility for a Squirrel language to generating wrapper-functions for C++ classes and have a problem with passing the pointer-to-member argument and deducing the type of them. I want to make a library which I can use like that: class_<fs::path, sq_filesystem::ttag>(v, "path",...
10
2253
by: Tony Johansson | last post by:
Hello Experts!! This class template and main works perfectly fine. I have this class template called Handle that has a pointer declared as T* body; As you can see I have a reference counter in the class template so I know how many references I have to the body. In my case it's the Integer wrapper class which is the body. This template...
1
1590
by: Raf256 | last post by:
I have base template class A<B>, and son class B. Inside A<B> constrcutor, can I access a pointer to B, from "this"? like A<B>::A() : pointerToB(static_cast<B*>this) { } I will use the pointer.
9
2313
by: Bern McCarty | last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something that we did in the old syntax that proved to be very valuable was to make sure that the finalizer would purposefully generate an assertion failure for unoptimized, debug builds. We did this to find and fix cases where we were relying upon finalization rather than...
3
12079
by: Lawrence Spector | last post by:
How does one call a templated constructor inside of a class when instantiating an object? I made up a quick sample to demonstrate. #include <iostream> class TestClass { public: template <class T> TestClass()
0
7475
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7409
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7918
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
715
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.