472,951 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,951 software developers and data experts.

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 1590
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.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - 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.********@comAcast.net> wrote in message
news:V7*******************@newsread1.mlpsca01.us.t o.verio.net...
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.********@comAcast.net> wrote in message
news:V7*******************@newsread1.mlpsca01.us.t o.verio.net...
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
by: usr2003 | last post by:
I wrote the following test program to test the linkage directives extern "C": #include <stdio.h> extern "C" {
5
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
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
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
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...
12
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
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...
3
by: Juha Nieminen | last post by:
Consider this code: void foo(int& i) { i += 10; } int main() { int a = 1;
6
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...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

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.