473,791 Members | 3,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template parameter type constructor method call

How do I write a constructor mehtod call in this case

/*-----------*/
template<typena me Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of
class T
}

};

CObjectPoolImpl <mynamespace::C MyTypeCF;
CMyType mt;

CF.smth(&mt);
/*-----------*/

MS Visual C++ 7.1:
error C2039: 'T' : is not a member of 'mynamespace::C MyType'

There is a reason not to write 'new T' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.

Oct 12 '06 #1
18 3667
AlexanderVX wrote:
How do I write a constructor mehtod call in this case
There is no "constructo r method call" in C++. What is it you're
trying to accomplish?
>
/*-----------*/
template<typena me Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of
class T
I am guessing here, but you might want to do

new (pObj) T();
}

};

CObjectPoolImpl <mynamespace::C MyTypeCF;
CMyType mt;

CF.smth(&mt);
This will very likely cause the constructor to be called twice for the
same object. That has undefined behaviour, AFAICT. Why do you think
you need it? 'mt' is already a fully constructed object.
/*-----------*/

MS Visual C++ 7.1:
error C2039: 'T' : is not a member of 'mynamespace::C MyType'

There is a reason not to write 'new T' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.
Read about "placement new".

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 12 '06 #2
AlexanderVX wrote:
How do I write a constructor mehtod call in this case

/*-----------*/
template<typena me Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of class T
new ( (void*) pObj ) T ();

or:

std::allocator< T>().construct ( pObj, T() );
}

};

CObjectPoolImpl <mynamespace::C MyTypeCF;
CMyType mt;

CF.smth(&mt);
/*-----------*/

MS Visual C++ 7.1:
error C2039: 'T' : is not a member of 'mynamespace::C MyType'

There is a reason not to write 'new T' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.
Note: probably, someonewill tell you that you cannot call a constructor and
that all that you can do is cause the implementation to invoke/call it.
That slogan refers to three facts: (a) constructor calls are not function
calls, (b) the standard, somewhat cryptically, states that constructors
have no names (hence they are not found through name lookup), and (c) the
standard uses the passive voice in talking about constructors being called.

Personally, I find it rather confusing to underscore the difference between
functions calls and constructor calls by taking the linguistic license to
say that we call functions but not taking the license to say that we call
constructors. (You take a linguistic license in either case since the
standard also uses the passive voice when talking about a function being
called: the standard is just not concerned with what programmers do. Also,
a function call according to the standard is just a certain form of
expression: all you do is to write such an expression in your program and
that causes the implementation to invoke the function in the course of
evaluating the expression.)
Best

Kai-Uwe Bux
Oct 12 '06 #3
This will very likely cause the constructor to be called twice for the
same object. That has undefined behaviour, AFAICT. Why do you think
you need it? 'mt' is already a fully constructed object.
You are right. This is just a sample. And the question was not about
it...
Read about "placement new".
I knew, but this kind of solution was not on the top of my head...

Oct 12 '06 #4
Kai-Uwe Bux, your philosophic remark helps understanding. Good I am not
on technical interview.

Oct 12 '06 #5
And I did it:
pObj = new ( (void*) pObj ) T ();

Guys, "placement new" solution results in

error C2059: syntax error : '('

I have an MFC project with a lot of headers included... What can cause
this?

Oct 12 '06 #6
AlexanderVX wrote:
And I did it:
pObj = new ( (void*) pObj ) T ();

Guys, "placement new" solution results in

error C2059: syntax error : '('

I have an MFC project with a lot of headers included... What can cause
this?
Probably not including <memory>

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 12 '06 #7
Probably not including <memory>

I am not quite sure why I should have included that STL header... BTW,
this no-good project include stuff even not allowing me to use none of
STL....

If I include <memoryor <listor whatever STL:

C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\xutility(341) : error C2084: function
'std::_Scalar_p tr_iterator_tag std::_Ptr_cat(c onst std::_Bool
*,std::_Bool *)' already has a body
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\xutility(251) : see previous definition of '_Ptr_cat'
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\xutility(335) : error C2084: function
'std::_Scalar_p tr_iterator_tag std::_Ptr_cat(s td::_Bool *,std::_Bool
*)' already has a body
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\xutility(245) : see previous definition of '_Ptr_cat'
C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\includ e\xutility(168) : error C2766: explicit specialization;
'std::iterator_ traits<std::_Bo ol>' has already been defined

And I am confused because it is not obvious what's causing it...

But now my question is why I cannot compile such a simple thing... This
requires include <memoryor something?

pObj = new ( (void*) pObj ) T ();

Oct 12 '06 #8
AlexanderVX wrote:
And I did it:
pObj = new ( (void*) pObj ) T ();
[snip]

Nope. Just

new ( (void*) pObj ) T ();

This is not allocating memory for you. The pointer pObj must already point
to the memory location where you want that object to be constructed.
Best

Kai-Uwe Bux
Oct 12 '06 #9
Kai-Uwe Bux, thanks, it removes unwanted assignment but compilation
error does not go away...

Oct 12 '06 #10

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

Similar topics

2
5006
by: Alexander Stippler | last post by:
Hello, I want to write a non-template class with a constructor template member. The constructor shall not take arguments. Is this possible at all? Here the concrete example: A class which contains three vectors. There are several different methods to initialize these vectors(size and contents). These methods are the template parameter I want to use. enum Method {A, B, C, D, E, F};
8
2248
by: Alexander Stippler | last post by:
Hello, short question: What is illegal about the following code? template <typename T> class Method { }; class Procedure
14
2905
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); template<typename T>
8
3154
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int, 1> and A<int, 2> are different types. Now, if the A template
12
2626
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
12
2328
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include <iostream> using namespace std; template <class ClassB> class ClassA
3
2037
by: mike.arsenault | last post by:
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.
0
1247
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
3
3584
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
0
9666
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10201
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10147
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9987
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9023
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6770
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2910
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.