473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using objects returned from functions

I'm having trouble using Braid (my user-defined type) objects returned
from functions. I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b). When a Braid
calls this, b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.

I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck (this may partly be because I
overloaded * to multiply Braids together?)

Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Thanks!

James McLaughlin.
Aug 1 '05 #1
6 1161
ze****************************@yahoo.com wrote:
I'm having trouble using Braid (my user-defined type) objects returned
from functions.
What kind of trouble?
I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b).
What does that mean "function needs to use"?
When a Braid
calls this,
'This' -- what?
b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.
Uh... With what problem?
I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.
What situation?
Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck (this may partly be because I
overloaded * to multiply Braids together?)
Of course. The object which 'descend_n' returns, is _a_temporary_.
It cannot be bound to a reference to non-const. Make your 'left_multiply'
accept a reference to const:

void left_multiply(Braid const&);
Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);
See above.
as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.


Read a bit more about const-correctness. That should give you a hint.

V
Aug 1 '05 #2

ze****************************@yahoo.com wrote:
I'm having trouble using Braid (my user-defined type) objects returned
from functions. I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b). When a Braid
calls this, b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.
Perhaps you should refresh yourself on what your code is actually
doing? Sounds like double dispatch (google if you don't know what it
is, but you need a strong understanding of object-oriented design to
understand what double dispatch is), but double dispatch makes use of
polymorphism, which your code has otherwise shown no indication of
having.

the whole point of passing something by reference (to non-const) is so
that the function modifies the paramater you pass it AND you do
something with the parameter afterwards:

void foo(double &x) { x = 3.0; }

int main() {
dobule y = 2;
foo(y); /* modify y */
std::cout << y << std::endl; /* do something with y afterwards */
}

There is no point in passing a temporary by reference (to non-const),
since you can't do anything with the resulting modifications:

void foo(double &x) { x = 3.0; }

int main() {
foo(std::pow(5.0, 2.0)); /* foo modifies the return value of
std::pow, but what's the point? you can't access the variable that was
just modified */
}

Note that the above program will NOT COMPILE. There is generally no
point in passing a temporary to a function by reference, and so the
compiler won't let you.

That pretty much sums up your situation and why you're getting the
errors.
I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck
Don't randomly try stuff. Computer science is a science, not guesswork.
If you don't know beforehand whether or not something will solve your
problem, then you need to read your book and get a better understanding
of the language.
(this may partly be because I
overloaded * to multiply Braids together?)
No. Unary and binary * are completely different operations. Read your
book.
Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Thanks!

James McLaughlin.


Why does left_multiply take a reference to non-const in the first
place? Should it take an object instead? should it take a reference to
const? Do you know?

Aug 1 '05 #3
ze****************************@yahoo.com wrote:
I'm having trouble using Braid (my user-defined type) objects returned
from functions. I've got:

Braid descend_n(int n, int no_strings)

This function needs to use void left_multiply(Braid&b). When a Braid
calls this, b will call void left_multiply_response(Braid& b2 - I've
forgotten for now how I did this but b2 is the braid that called
l-multiply). This may be something to do with the problem, I don't
know.

I've also tried changing the function return type from Braid to Braid&
without affecting the situation at all.

Anyway, I've tried to use b.left_multiply(descend_n(n-1, no_strings));
in my code. Whereupon I get a compiler error message saying I can't
pass a Braid, only a Braid&, as a parameter. I've tried putting * and
& before descend_n with no luck (this may partly be because I
overloaded * to multiply Braids together?)

Can anyone tell me what to do that wouldn't require me to try
something like
Braid b3=descend_n(ETC);
b.left_multiply(b3);

as I have a feeling being able to use the function results directly
could be very useful a lot of times in the new functions I'm writing
now.

Thanks!

James McLaughlin.


Posting the definition for left_multiply would help more.

Anyways...maybe moving left multiply to using a Braid vs Braid&.
(You're using a reference to a temporary object. This could end up
having undesired side effects).

Perhaps a post of the left_multiply and descend code would also help.

Aug 1 '05 #4
Geo

Alipha wrote:
Don't randomly try stuff. Computer science is a science, not guesswork.
If you don't know beforehand whether or not something will solve your
problem, then you need to read your book and get a better understanding
of the language.


Is it 'guesswork' or is it 'experimentation', you shouldn't be so
judgemental, sometimes experimenting is the best way to learn, if we
only stuck to stuff we read in books, we'd never move forward, and if
we knew beforehand what the solution was, then life really would be
boring !!!!

Aug 1 '05 #5
Geo wrote:
Alipha wrote:
Don't randomly try stuff. Computer science is a science, not guesswork.
If you don't know beforehand whether or not something will solve your
problem, then you need to read your book and get a better understanding
of the language.


Is it 'guesswork' or is it 'experimentation', you shouldn't be so
judgemental, sometimes experimenting is the best way to learn, if we
only stuck to stuff we read in books, we'd never move forward, and if
we knew beforehand what the solution was, then life really would be
boring !!!!


I'll admit, it personally took me some fiddling around to get the hang
of pointers, & operator, and *. It was confusing at first, and no
matter how much I read, you just really can't get the hang of it til
you start trying to use it in code. Granted, it all got caught by the
compiler, so no hassle trying to find deep problems, but if you're just
playing around, experimentation with thorough tests is a great way to
learn.

Aug 1 '05 #6
Thanks (especially to Geo and Josh for friendly postings!) I'm now
passing const Braid& into my functions and they work perfectly.
Aug 5 '05 #7

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

Similar topics

7
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on...
0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
28
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()',...
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
115
by: junky_fellow | last post by:
What is a C object ? If i have some function "func()" in my C program, then can i say that "func()" is a C object ? or if i have some function pointer (ptr) which contains the address of...
2
by: Claire | last post by:
If I need to create thousands of objects of the same type, is it more economical (memory wise) to use static member functions where possible? How about virtual ones?
7
by: fakeprogress | last post by:
For a homework assignment in my Data Structures/C++ class, I have to create the interface and implementation for a class called Book, create objects within the class, and process transactions that...
3
by: Divick | last post by:
I was reading this section in Bruce Eckel's book which talks about passing and returning large objects ( Chapter 11: References & the Copy-Constructor ), where he explains that how to return big...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.