473,806 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Does "void func(MyClass *& myCls)" make sense?

I saw someone use the following code:

void func(MyClass *& myCls)
{
myCls->test();
}

// call func():
func(new MyClass);

Some compiler could compile, but some not.

I don't understand the meaning of function prototype. I guess it's not
a stand C++ prototype. Please don't guess like me if you are not 100%
understanding it :-) because probably I'll have further question if
you answer.
Jul 23 '05 #1
5 1629
modemer wrote:
void func(MyClass *& myCls)
{
myCls->test();
}
The above code is OK: it is a function taking a pointer by reference.
It is apparently unnecessary to pass the argument by reference,
though, as it is not changed. If would be a better example if it had
a body like this:

{
myCls = new MyClass();
}
// call func():
func(new MyClass);


However, no compiler shall accept this code with the above
declaration. The function's argument is a temporary pointer to an
object but temporaries cannot be bound to non-const references.
--
<mailto:di***** ******@yahoo.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - Software Development & Consulting

Jul 23 '05 #2
modemer wrote:
I saw someone use the following code:

void func(MyClass *& myCls)
{
myCls->test();
}

// call func():
func(new MyClass);

Some compiler could compile, but some not.

I don't understand the meaning of function prototype. I guess it's not
a stand C++ prototype. Please don't guess like me if you are not 100%
understanding it :-) because probably I'll have further question if
you answer.


There is no prototype here. The function 'func' as defined/declared,
takes one argument which is a reference to a non-const pointer to
an object of MyClass.

A Standard-compliant compiler should not compile

func(new MyClass);

because the 'new' expression returns an r-value and a reference to
a non-const pointer cannot be bound to an r-value. You can change
it to

void func(MyClass * const & myCls)
{
myCls->test();
}

then all compilers should compile

func(new MyClass);

, however, this will be a memory leak because the value of the pointer
obtained from 'new' is lost and the memory is never freed and the object
is never destroyed.

V
Jul 23 '05 #3
Victor Bazarov wrote:

A Standard-compliant compiler should not compile

func(new MyClass);


My usual comment: A Standard-compliant compiler must issue a diagnostic.
The standard doesn't say that this should not compile. Similarly,
Dietmar's "no compiler shall accept this code..." should be "no compiler
shall accept this code without issuing a diagnostic...". Once it issues
a diagnostic the compiler is free to do whatever the compiler writer
wants, including generating an executable file. Of course, the standard
doesn't say what such an executable file should do.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #4
"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:V7******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
modemer wrote: void func(MyClass * const & myCls)
{
myCls->test();
}

then all compilers should compile

func(new MyClass);

, however, this will be a memory leak because the value of the pointer
obtained from 'new' is lost and the memory is never freed and the object
is never destroyed.


That has been exactly my comment after reviewing some code at a previous
company.

It turned out that the member function (here 'test') was actually
registering the 'this' pointer in some static container and such objects
were in fact being deleted later on. Argh!

Memory leak or not, I still know that it's an error to design like that :)

Ali

Jul 23 '05 #5
Victor,

Such appreciate your perfect explanation about "*&"!!!

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:V7******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
modemer wrote:
I saw someone use the following code:

void func(MyClass *& myCls)
{
myCls->test();
}

// call func():
func(new MyClass);

Some compiler could compile, but some not.

I don't understand the meaning of function prototype. I guess it's not
a stand C++ prototype. Please don't guess like me if you are not 100%
understanding it :-) because probably I'll have further question if
you answer.


There is no prototype here. The function 'func' as defined/declared,
takes one argument which is a reference to a non-const pointer to
an object of MyClass.

A Standard-compliant compiler should not compile

func(new MyClass);

because the 'new' expression returns an r-value and a reference to
a non-const pointer cannot be bound to an r-value. You can change
it to

void func(MyClass * const & myCls)
{
myCls->test();
}

then all compilers should compile

func(new MyClass);

, however, this will be a memory leak because the value of the pointer
obtained from 'new' is lost and the memory is never freed and the object
is never destroyed.

V

Jul 23 '05 #6

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

Similar topics

4
3271
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
5
2680
by: Nimmi Srivastav | last post by:
When I was learning C, I learned that the data type of a function name by itself is a function pointer. For example int someFunction(char* str) { .... }
6
4368
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
4
2503
by: Jian H. Li | last post by:
Hello, What's the essential differences between the two ways of "class::member" & "object.member"(or object_pointer->member)? class C{ public: void f() {} int i; };
6
2617
by: Otto Wyss | last post by:
I've the following function declaration: wxTree GetLastChild (const wxTree& item, long& cookie) const; I'd like to make the cookie parameter optional, i.e. "long& cookie = ....", without breaking any software using the old API. The implementation looks like wxTree wxTreeListMainWindow::GetLastChild (const wxTree& item, long& cookie) const {
12
2625
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
4
14813
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following expression: .... snip ...
3
2272
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
6
5592
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less obtuse. I'm puzzled why this one programmer insisted on odd style everywhere. Maybe he's just...
0
9719
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
9598
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,...
1
10373
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
10111
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
9192
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
7650
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
6877
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
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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

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.