473,797 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can you declare function f that...

How do you declare a function f that takes a parameter that is a pointer
to itself?

So you can do f(f);

Pardon my curiosity!

Ben
Dec 15 '06 #1
5 1318


On Dec 14, 10:57 pm, benben <benhonghatgmai ldotcom@nospamw rote:
How do you declare a function f that takes a parameter that is a pointer
to itself?

So you can do f(f);

Pardon my curiosity!

Ben
struct Boo { template<class T>Boo( T ); };

void f( Boo );

void foo()
{
f( f );
}

Dec 15 '06 #2
Pardon my curiosity! but can anybody please explain why this code
works?

-softcoder
Vyacheslav Kononenko wrote:
On Dec 14, 10:57 pm, benben <benhonghatgmai ldotcom@nospamw rote:
How do you declare a function f that takes a parameter that is a pointer
to itself?

So you can do f(f);

Pardon my curiosity!

Ben

struct Boo { template<class T>Boo( T ); };

void f( Boo );

void foo()
{
f( f );
}
Dec 15 '06 #3
"softcoder" <ms*****@gmail. comwrote in message
news:11******** *************@j 72g2000cwa.goog legroups.com
>
Vyacheslav Kononenko wrote:
>On Dec 14, 10:57 pm, benben <benhonghatgmai ldotcom@nospamw rote:
>>How do you declare a function f that takes a parameter that is a
pointer to itself?

So you can do f(f);

Pardon my curiosity!

Ben

struct Boo { template<class T>Boo( T ); };

void f( Boo );

void foo()
{
f( f );
}

Pardon my curiosity! but can anybody please explain why this code
works?

-softcoder

Does it? At least on my compiler, it fails to link. This is easily corrected
with a couple of empty definitions, but it still doesn't give the results
one might want.

struct Boo { template<class T>Boo( T ); };

which I would re-write as:

struct Boo
{
template<class T>
Boo( T );
};

gives Boo a templated constructor. You can pass Boo's constructor an
argument of any type and a constructor that takes that type will be produced
by the template mechanism. Thus, when you pass it f, the template parameter
T becomes f.

void f( Boo );

defines f to accept a Boo parameter. Thus when

f(f);

is called, f is converted to a Boo temporary using Boo's templated
constructor and this Boo temporary is passed to f (or at least it would be
if the compiler didn't optimise it away).

Accordingly, while f is used as the function argument, the f function never
gets to see this argument, which rather defeats the purpose (at least you
would think so; the OP didn't explain the purpose beyond satisfying his
curiousity).

Observe that recursive functions call themselves without needing a function
pointer to themselves, so it is not clear what the point of

f(f);

might be.

With function objects, you can achieve something that "works" after a
fashion:

struct F
{
F() : count(0)
{}
void operator()(F& f)
{
cout << "f\n";
++count;
if(count < 10)
f(*this);
}
int count;
};

int main()
{
F f;
f(f);

return 0;
}

--
John Carson
Dec 15 '06 #4
On Fri, 15 Dec 2006 14:57:51 +1100, benben wrote:
How do you declare a function f that takes a parameter that is a pointer
to itself?

So you can do f(f);
struct ftype
{
void operator() (ftype) const {}
};

int main() {
ftype f;
f(f);
}

Well ok, so functor, not function... and it wasn't actually supposed to
do anything was it?

--
Lionel B
Dec 15 '06 #5
Lionel B wrote:
On Fri, 15 Dec 2006 14:57:51 +1100, benben wrote:
>How do you declare a function f that takes a parameter that is a pointer
to itself?

So you can do f(f);

struct ftype
{
void operator() (ftype) const {}
};

int main() {
ftype f;
f(f);
}

Well ok, so functor, not function... and it wasn't actually supposed to
do anything was it?
No it's just I am a bit curios about this, nothing big deal at all. I'm
thinking something like

void f( PTR_TO_FUNC inner)
{
if (inner == 0)
return;

if (...) inner(0);
else inner(inner);
}

Ya I know it's trivial with functor, but I'm just so curios if anyone
can do it with a straight function :D

Ben
Dec 15 '06 #6

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

Similar topics

2
4746
by: Not Me | last post by:
Hi, I'm trying to create a function that returns a table, however I want to use a local variable in there and enterprise manager ain't liking it! The error I get is number 156 'incorrect syntax near the keyword 'declare'.. hopefully this is just a simple thing where I've put it in the wrong place. The code follows: CREATE FUNCTION AFGroupedTotals (@campaign nvarchar(30),@datefrom
6
19063
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the external file, but I keep getting an error about the object does not exist. Can someone tell me where or how to declare a global variable in an external file that is available after the page is loaded.
0
2679
by: Walti | last post by:
Hi I'm trying to interface a foreign DLL that is written in Delphi. I know: - Delphi-Integer is a 32-Bit signed Integer --> VB: LONG - Delphi-PChar is a Pointer to Zero-Terminated ASCII String --> I assume VB: String The Delphi Declaration looks as follows: function MP_GetMessageList( HDL : Integer; var ErrCount : Integer;
2
12556
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any is not supported in a declare statement" during the automated Conversion process. Some examples of this are: Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory"(ByRef
5
4611
by: Sakharam Phapale | last post by:
Hi All, How to declare the following statement in following structure. szPname As String * MAXPNAMELEN Public Structure MIXERCAPS public wMid As Integer public wPid As Integer public vDriverVersion As Long
3
4944
by: Altman | last post by:
I am having problems with Declaring a function from a dll. I had it work in VB 6 but I can't get it to work in VB.net. This was the call in VB6 Public Declare Function MBTConnect Lib "MBT" (ByVal szHostAddress As String, ByVal port As Integer, ByVal useTCPorUDP As Long, ByVal requestTimeout As Long, hSocket As Long) As Long When I try this in VB.net I get the following Error: "Object Reference not set to an instance of an object" ...
1
4465
by: marfi95 | last post by:
I'm trying to call methods in a C++ dll from vb.net that is not a .com component. I've read I can just use regular Declare statements just as you do in VB6 except that some of the types need to be converted in the declaration. Is there any doco somewhere that identifies what needs to be changed in
6
2134
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? One reason is that the ByRef or ByVal does not show by InteliSence so the "p" would help there. ------ Secondly, I'd like to be consistent with the parameter names. I'd like to
2
2505
by: Igor Shevchenko | last post by:
Hi. I've got a problem with using cursor in a plpgsql function. Cursor is created via DECLARE, it's SELECT query has placeholders. I use PostgreSQL 7.4.2. Here's a problematic plpgsql function: create or replace function add_messages_to_folder(integer,refcursor) returns integer security definer as '
4
1229
by: **Developer** | last post by:
I saw the following in an example Public Overloads Declare Auto Function CreateDC Lib "gdi32" (ByV...As Integer... Public Overloads Declare Auto Function CreateDC Lib "gdi32" (ByV...As String... I know what Overloads means in relation to a function in the base class.
0
9685
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
9536
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
10245
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
10021
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
9063
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
7559
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
5458
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2933
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.