473,385 Members | 1,357 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,385 software developers and data experts.

is class visible, is this the correct ?

I want to pass a function (testfn) into a another function (fnmeas).
testfn makes use of an object cx, so is it ok to have cx local to the
file containing testfn ? The fnmeas does not care what cx is as such,
but somehow the scope does not seem right.

example

typedef double fn(const double &x);

in fns.cpp..

myclass cx;

double testfn(const double &y)
{
return y*cx.a*cx.b;
}

void go()
{
cx.a = 1;
cx.b = 2;
fn *p = &testfn;
double x = fnmeas(p);
}

in cv.cpp..

double fnmeas(fn *f)
{
return f(1.5) + f(2.5);
}

Is it better to pass a handle of cx to fnmeas ? How would I do that ?

I may be thinking in non OO terms here, but the idea is to have fnmeas
generic.

Oct 3 '06 #1
2 1582
Greg wrote:
I want to pass a function (testfn) into a another function (fnmeas).
testfn makes use of an object cx, so is it ok to have cx local to the
file containing testfn ? The fnmeas does not care what cx is as such,
but somehow the scope does not seem right.

example

typedef double fn(const double &x);

in fns.cpp..

myclass cx;

double testfn(const double &y)
{
return y*cx.a*cx.b;
}

void go()
{
cx.a = 1;
cx.b = 2;
fn *p = &testfn;
double x = fnmeas(p);
}

in cv.cpp..

double fnmeas(fn *f)
{
return f(1.5) + f(2.5);
}

Is it better to pass a handle of cx to fnmeas ? How would I do that ?

I may be thinking in non OO terms here, but the idea is to have fnmeas
generic.
One way would be to use function objects and templates, similar to what the
standard library does. Maybe something like this (untested):

struct testfn
{
testfn(myclass& obj)
: obj_(obj)
{
}

double operator()(const double& y) const
{
return y*obj_.a*obj_.b;
}

myclass& obj_:
};
void go()
{
myclass cx;
cx.a = 1;
cx.b = 2;
testfn p(cx);
double x = fnmeas(p);
}

template<typename T>
double fnmeas(T f)
{
return f(1.5) + f(2.5);
}

Oct 3 '06 #2

Rolf Magnus wrote:
One way would be to use function objects and templates, similar to what the
standard library does. Maybe something like this (untested):

struct testfn
{
testfn(myclass& obj)
: obj_(obj)
{
}

double operator()(const double& y) const
{
return y*obj_.a*obj_.b;
}

myclass& obj_:
};
void go()
{
myclass cx;
cx.a = 1;
cx.b = 2;
testfn p(cx);
double x = fnmeas(p);
}

template<typename T>
double fnmeas(T f)
{
return f(1.5) + f(2.5);
}
Thanks, templates can be neat. I put everything in the class and then
made "testfn" virtual, but I might use a template later.

Oct 3 '06 #3

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

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
16
by: Stoic | last post by:
Hi, I'm relatively new to CSS. I'm trying to get all the urls in a group of text bracketed by <p></p> tags to be underlined and everywhere else to NOT be underlined. As of now my code is like...
3
by: kchalla | last post by:
Can anybody let me know the difference between public and private class access modifiers? Thanks in advance
30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
5
by: Lyn | last post by:
Hi, I have a form which contains a number of subforms in different pages of a tab control. In the detail section of each subform, I list records related to the main form record, but from...
12
by: Sunny | last post by:
Hi All, I have a serious issue regarding classes scope and visibility. In my application, i have a class name "TextFile", and also a few other classes like "TotalWords", "TotalLines" and etc..,...
6
by: Orgun | last post by:
Hi, I sent this message to the moderated c++ group too but it is waiting for moderator approval and I wanted to send here too. I am new to Design Patterns. I want to write a simple...
8
by: gerry | last post by:
The PagerSettings.Visible property is not being handled properly by the GridView control. The value assigned to this property is not applied until after a postback. Simplest test : .aspx...
8
by: Doc John | last post by:
I have an MDI container with a child Form which will be visible according to certain events. The problem is that when I set the property Visible to False and then back to True, the Form will be in...
8
by: Mayur H Chauhan | last post by:
All, For my knowledge, if I declare Class as follow, then it thows compilation error. Protected Class Book End Class Even same for...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...

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.