473,788 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Prohibit pass by value

Dear all,
can I prohibit that my class will be passed by value (i.e created a copy on
stack when function is called)? I want to prohibit passing my class by
value. I thougth that declare a default constructor to private would do the
job, but no.
When myfunc is called some construcor goes called, but not the explicit
one.I have noidea which one is it. I am running out of ideas.
With best wishes,
Boni
Any ideas?
template< typename T> void myfunc(const templclass<T> a){

...

}
template< typename T> struct templclass

{

explicit templclass(std: :string Name_, T DefaultVal_=0)

{

cout<<"bhcadshh vfah"<<endl;

};

std::ostream& operator<< (std::ostream& os)const {

return os<<m_Value;

};

//assignment operator

templclass& operator = ( const T a ){

m_Value=a;

return *this;

}

operator T ( ){

return m_Value;

}

private:

templclass();

T m_Value;

templclass( T);
};
Mar 1 '06 #1
4 1207
Firstly, I'd be interested in knowing *why* you want to prevent this, since
that seems more like the problem to me.

Kevin

"Boni" <no****@parampa m.pam> wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...
Dear all,
can I prohibit that my class will be passed by value (i.e created a copy
on stack when function is called)? I want to prohibit passing my class by
value. I thougth that declare a default constructor to private would do
the job, but no.
When myfunc is called some construcor goes called, but not the explicit
one.I have noidea which one is it. I am running out of ideas.
With best wishes,
Boni
Any ideas?
template< typename T> void myfunc(const templclass<T> a){

..

}
template< typename T> struct templclass

{

explicit templclass(std: :string Name_, T DefaultVal_=0)

{

cout<<"bhcadshh vfah"<<endl;

};

std::ostream& operator<< (std::ostream& os)const {

return os<<m_Value;

};

//assignment operator

templclass& operator = ( const T a ){

m_Value=a;

return *this;

}

operator T ( ){

return m_Value;

}

private:

templclass();

T m_Value;

templclass( T);
};

Mar 1 '06 #2
Hi Kevin,
I found the solution, the constructor signature was wrong. should be
templclass( const T&); and also assignment op should be declared as private.
Now to your question. Sometimes you want to prohibit user to create objects
implicitely. Example: such object will be incorrectly initialized (as I
wrote compiler generates constructor without arguments) . There are many
such situations.
regards,
Boni
"Kevin Frey" <ke**********@h otmail.com> schrieb im Newsbeitrag
news:em******** ******@TK2MSFTN GP11.phx.gbl...
Firstly, I'd be interested in knowing *why* you want to prevent this,
since that seems more like the problem to me.

Kevin

"Boni" <no****@parampa m.pam> wrote in message
news:%2******** **********@TK2M SFTNGP14.phx.gb l...
Dear all,
can I prohibit that my class will be passed by value (i.e created a copy
on stack when function is called)? I want to prohibit passing my class by
value. I thougth that declare a default constructor to private would do
the job, but no.
When myfunc is called some construcor goes called, but not the explicit
one.I have noidea which one is it. I am running out of ideas.
With best wishes,
Boni
Any ideas?
template< typename T> void myfunc(const templclass<T> a){

..

}
template< typename T> struct templclass

{

explicit templclass(std: :string Name_, T DefaultVal_=0)

{

cout<<"bhcadshh vfah"<<endl;

};

std::ostream& operator<< (std::ostream& os)const {

return os<<m_Value;

};

//assignment operator

templclass& operator = ( const T a ){

m_Value=a;

return *this;

}

operator T ( ){

return m_Value;

}

private:

templclass();

T m_Value;

templclass( T);
};


Mar 1 '06 #3
Boni wrote:
Dear all,
can I prohibit that my class will be passed by value (i.e created a copy on
stack when function is called)? I want to prohibit passing my class by
value. I thougth that declare a default constructor to private would do the
job, but no.
You need to make the copy constructor private.
When myfunc is called some construcor goes called, but not the explicit
one.I have noidea which one is it. I am running out of ideas.
The copy constructor is auto-generated by the compiler, and that's the
one that is being called.

template< typename T> struct templclass

{

explicit templclass(std: :string Name_, T DefaultVal_=0)

{

cout<<"bhcadshh vfah"<<endl;

};
There shouldn't be a ; after a function definition.
std::ostream& operator<< (std::ostream& os)const {

return os<<m_Value;

};

//assignment operator

templclass& operator = ( const T a ){

m_Value=a;

return *this;

}

operator T ( ){

return m_Value;

}

private:

templclass();

T m_Value;

templclass( T);


Why do you have that private constructor?

Add the private copy constructor here:
templclass(temp lclass const&); // no definition

You should probably also add the default copy-assignment operator:
templclass& operator=(templ class const&); //no definition

Tom
Mar 1 '06 #4
Boni wrote:
Hi Kevin,
I found the solution, the constructor signature was wrong. should be
templclass( const T&); and also assignment op should be declared as private.
Now to your question. Sometimes you want to prohibit user to create objects
implicitely. Example: such object will be incorrectly initialized (as I
wrote compiler generates constructor without arguments) . There are many
such situations.


To give you the best solution:
http://www.boost.org/libs/utility/ut...ss_noncopyable

Tom
Mar 1 '06 #5

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

Similar topics

0
12089
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the likelihood of CRM success from less than 20 percent to 60 percent. WHITEPAPER :
4
1892
by: antonyliu2002 | last post by:
Let me try to make clear what my concern is. I think it is a pretty interesting one, which I think of while I am developing my web application. I have an authenticated/authorized web application. People have to login from http://mydomain.com/ to access the information on my site. For now, this is working fine. People cannot bypass the login form, any attempt to check out a page (if they happen to know the file name) will be...
9
1718
by: bonk | last post by:
Does anyone have a simple example on how to prohibit that any thread other than the current thread modifies a certain object (a collection) while we are in a certain section of the code? In other words: while we are inside this codeblock whoever might think of modified that particular collection has to wait until we have left that codeblock. As far as I understand it lock() {} only prohibits that other threads enter a certain block of...
4
2293
by: Jon Slaughter | last post by:
I'm reading a book on C# and it says there are 4 ways of passing types: 1. Pass value type by value 2. Pass value type by reference 3. Pass reference by value 4. Pass reference by reference. My interpretation: 1. Essentially pushes the value type on the stack
14
20413
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is passed also. I understand that this way of passing the array is by value and if the prototype is declared as foo(int *), it is by reference in which case the value if modified in the function will get reflected in the main function as well. I dont...
10
13663
by: Robert Dailey | last post by:
Hi, I noticed in Python all function parameters seem to be passed by reference. This means that when I modify the value of a variable of a function, the value of the variable externally from the function is also modified. Sometimes I wish to work with "copies", in that when I pass in an integer variable into a function, I want the function to be modifying a COPY, not the reference. Is this possible?
0
1026
by: scissors | last post by:
Is there a way in XML Schema to prohibit the use of <redefine>?? Thanks in advance
6
2714
by: lisp9000 | last post by:
I've read that C allows two ways to pass information between functions: o Pass by Value o Pass by Reference I was talking to some C programmers and they told me there is no such thing as pass by reference in C since you are just passing an address (or a pointer value address I guess?). So I was wondering is this correct?
12
11111
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms. Here is a newbie mistake that I found myself doing (as a newbie), and that even a master programmer, the guru of this forum, Jon Skeet, missed! (He knows this I'm sure, but just didn't think this was my problem; LOL, I am needling him) If...
0
9498
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
10172
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...
0
9967
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
8993
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...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6750
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
5398
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
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.