CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2009
    Posts
    4

    Static library using virtual methods

    Hi,

    I'm trying to create a static lib.

    This lib needs to read/write to a db, but the library has no information about db (type, pass, etc).

    The idea is to create an interface that the library client will fill to provide to the library DB stuff.


    ------------------------- class IDBProvider --------------------------------------
    #include "IDBResult.h"

    using std::string;


    class IDBProvider{
    public:
    virtual IDBResult * executeSQLCommand(string command);
    };

    ------------------------- class IDBResult --------------------------------------

    class IDBResult{
    public:

    /**
    * Retrieves the value of the designated column in the current row of this object
    * as a long.
    */
    virtual long getLong(string columnLabel);

    /**
    * Retrieves the value of the designated column in the current row of this object
    * as a int.
    */
    virtual int getInt(string columnLabel);
    };


    ----------------------------- staticLibImplementation.h ----------------------

    #include #include "IDBProvider.h"
    bool doLibraryStuff(long *result, IDBProvider db);

    ----------------------------- staticLibImplementation.cpp ----------------------

    bool doLibraryStuff(long *result, IDBProvider db){
    db.executeSQLCommand("A_QUERY_TO_DB");
    }


    I'm creating the library as follows :

    g++ -Wall -g -c -o libDB.o staticLibImplementation.cpp
    ar rcs libDB.a libDB.o


    It compiles ok, but if I do a "nm libDB.a" I can see that:

    U _ZN11IDBProvider17executeSQLCommandESs

    So IDBProvider::executeSQLCommand is undefined.


    Then , if I try to compile a test that uses libDB.a, at linking phase complains about unreferenced symbols.


    The qüestion is: Can I create different interfaces like this and use it in a static lib? I'm new in c++ (I come from java) and I don't really don't know about this. How I should compile it?

    Thanks in advance.

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Static library using virtual methods

    To create a pure virtual function:
    Code:
    virtual int getInt(string columnLabel) = 0;
    Please use code tags.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Aug 2009
    Posts
    4

    Re: Static library using virtual methods

    But If I use pure virtual, when I try to compile the lib source , the compiler complains about declaring the type "IDBProvider" inside the method:

    bool doLibraryStuff(long *result, IDBProvider db);


    The compiler output is : (its a translation, I don't have it in english)

    The parameter db can not be declared as a abstract type IDBProvider.

  4. #4
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Static library using virtual methods

    So then IDBProvider is an abstract type - you either made it inherit from a class with a pure virtual function and did not override it, or you gave it a pure virtual function directly. Your options are to either make the db parameter a pointer or reference (or a smart pointer).
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured