Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8297357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:23:28+00:00 2026-06-08T15:23:28+00:00

I have a pure virtual class CF that denotes any continuous function in 1D

  • 0

I have a pure virtual class CF that denotes any continuous function in 1D it is defined a header file

// tools.h
class CF
{
public:
    virtual double operator()(double x) const=0 ;
};

what I intend to do is to wrap a couple of simple functions using functors and have them defined inside tools.h. If I use anonymous classes, like:

// tools.h
class : public CF{
public:
    virtual double operator()(double x) const { return 0.0; }
} zero;

then I run into trouble sine the class is redefined everywhere tools.h is included. gcc complains:

// tools.h included in main.cpp, foo.cpp, and bar.cpp
foo.o:(.bss+0x0): multiple definition of `zero'
main.o:(.bss+0x0): first defined here
bar.o:(.bss+0x0): multiple definition of `zero'
main.o:(.bss+0x0): first defined here

I figured I could fix this by doing something like:

// tools.h
class zero_: public CF{
public:
    virtual double operator()(double x) const { return 0.0; }
};

const static zero_ zero;

but I don’t like it since zero_ is visible everywhere and I don’t need it to be! How can I fix this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T15:23:30+00:00Added an answer on June 8, 2026 at 3:23 pm

    By not doing it. Just have

    struct zero : public CF {
      double operator()(double x) { return 0.; }
    };
    

    And use it with

    void fun(const CF& cf);
    fun(zero());
    

    One of the reasons to have functors over functions is their ability to carry and expose state through operations, your approach would throw that away for gaining 2 parenthesis in conciseness.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function X() declared as PURE VIRTUAL in base class: class Base
I have an abstract class with a pure virtual function f() and i want
I know that it's OK for a pure virtual function to have an implementation.
I have some abstract class called IClass (has pure virtual function). There are some
C++: I have a base class A with a pure virtual function f() and
Possible Duplicate: pure virtual function and abstract class I have a class and I
If I have a base class with a pure-virtual function, and a derived class
my question is rather simple, if you have an pure virtual class (interface) but
Do interfaces (polymorphic class solely with pure virtual functions) have a vtable? Since interfaces
Suppose I have a pure virtual method in the base interface that returns to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.