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

The Archive Base Latest Questions

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

I have tried to use this code in VS2008 (and may have included too

  • 0

I have tried to use this code in VS2008 (and may have included too much context in the sample…):

class Base
{
    public:
    void Prepare() {
       Init();
       CreateSelectStatement();
       // then open a recordset
    }
    void GetNext() { /* retrieve next record */ }
    private:
    virtual void Init() = 0;
    virtual string CreateSelectStatement() const = 0;
};
class A : public Base
{
   public:
   int foo() { return 1; }
   private:
   virtual void Init() { /* init logic */ }
   virtual string CreateSelectStatement() { /* return well formed query */ }
};

template<typename T> class SomeValueReader : protected T
{
   public:
   void Prepare() { T::Prepare(); }
   void GetNext() { T::GetNext(); }
   T& Current() { return *this; } // <<<<<<<< this is where it is interesting
   SomeValue Value() { /* retrieve values from the join tables */ }
   private :
   string CreateSelectStatement() const
   {
   // special left join selection added to T statement
   }
};

void reader_accessAmemberfunctions_unittest(...)
{
   SomeValueReader<A> reader();
   reader.Prepare();
   reader.GetNext();
   A a = reader.Current();
   int fooresult = a.foo();
   // reader.foo()            >> ok, not allowed
   Assert::IsEqual<int>( 1, fooresult );
};

This works as expected, i.e. having access to “A” member functions and fooresult returning 1. However, an exception is thrown when objects are deleted at the end of the unittest function:

System.AccessViolationException:
Attempted to read or write protected
memory. This is often an indication
that other memory is corrupt

If I change the return type of Current() function to :

T* Current()
{
   T* current = dynamic_cast<T*>(this);
   return current;
}

then everything is ok and the unit test ends with no access violation. Does someone can tell me what was wrong with the first Current() implementation? Thanks, bouchaet.

  • 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-13T01:38:33+00:00Added an answer on May 13, 2026 at 1:38 am

    After changing CreateSelectStatement to return a value for the implemented functions (not the pure virtual)

    string CreateSelectStatement() const { return ""; }
    

    and changing the declaration of reader (the declaration you have should strictly be interpreted as a function prototype in C++)

    SomeValueReader<A> reader;
    

    The above example compiles and executes without errors using gcc, leading me to believe the actual error may not be present in the source above. Unfortunately I’m unable to test with VC at the moment.

    I can’t see any obvious reason why the change you have suggested would resolve the issue, the only other error I can see is that Base does not have a virtual destructor declared meaning that if you ever delete a Base* (or some other derived class that is not the actual type) the incorrect destructor(s) will fire. You should declare it as

    virtual ~Base() {}
    

    even if it’s empty.

    Stylistically it’s also a little odd to use templates and virtual functions in this fashion because here you’re using the template to resolve functions at compile time. I can’t see a reason why SomeValueReader needs to derive from T (rather than have a member variable) either.

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

Sidebar

Related Questions

I have this code and it crashes every 1-2th launch. I have tried use
I have tried to use this code (as I have seen in a tutorial)
I have tried to use the following snippet of code: int main() { string
Code snippet to follow. I have a struct (example code has class, tried both,
i am trying to use this code. public synchronized void onActivityResult(final int requestCode, int
I have tried to use the following code to passing value back from the
I have a .net 3.5 project in vs2008 and I'm trying to use this
I have tried to use mysql fulltext search in my intranet. I wanted to
I have heard about JNI and have tried to use it. What I would
Have you tried to use SharePoint with version control such as Perforce (or Subversion),

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.