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 683753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:44:32+00:00 2026-05-14T01:44:32+00:00

for some reason I’m thinking on implementing interface within a some function(method) as local

  • 0

for some reason I’m thinking on implementing interface within a some function(method) as local class.

Consider following:

class A{
public:

    virtual void MethodToOverride() = 0;

};

A * GetPtrToAImplementation(){

    class B : public A {
    public:
        B(){}
        ~B(){}

        void MethodToOverride() {
            //do something
        }
    };

    return static_cast<A *>(new B());
}


int _tmain(int argc, _TCHAR* argv[])
{

    A * aInst = GetPtrToAImplementation();

    aInst->MethodToOverride();

    delete aInst;
    return 0;
}

the reasons why I’m doing this are:

  1. I’m lazy to implement class (B) in separate files
  2. MethodToOverride just delegates call to other class
  3. Class B shouldn’t be visible to other users
  4. no need to worry about deleting aInst since smart pointers are used in real implementation

So my question is if I’m doing this right?

Thanks in advance!

  • 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-05-14T01:44:32+00:00Added an answer on May 14, 2026 at 1:44 am
    1. You could define B in the unnamed namespace of the implementation file where you implement GetPtrToAImplementation().

    2. A should have a virtual dtor.

    3. By the current C++ standard, you cannot use local classes as template arguments. (Which means you can’t use them with the STL, for example.)

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

Sidebar

Related Questions

for some reason when I try to call CocoaAsyncSocket's onSocket:didReadData:withTag method, it's failing and
For some reason when I run the test method, I am getting a false
For some reason (probably because i don't understand closures) function inResult always returns false
For some reason if I try to display data with the following code, I
For some reason, exceptions thrown in an __autoload function aren't being caught when trying
For some reason when I add removeUpdates() after the requestLocationUpdates() method, I don't obtain
For some reason, my following line of code is returning a 0. int bac
For some reason this function is doing something weird just in Firefox function fadeOUT_sect(id)
For some reason the following code is giving me an exception. <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries>
For some reason, Firefox is throwing function not defined errors at this piece of

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.