The|Godfather wrote:
> I read Scotte Meyer's "Effective C++" book twice and I know that
> he mentioned something specific about constructors and destructors that
> was related to the
> following error/warning: "error: invalid use of nonstatic data member "
>
> However, he did NOT mention this error in the book explicitly.
Huh? Scotte (or Scott, as most people call him) mentioned something
specific related to that error but he did not mention that error
explicitly, so how do you know he was referring to that error
specifically?
> It
> happens always in the constructor when you try to initialize some data
> members in the constructor and try to accsess other data members.
You can't *initialize* static data members in the ctor
(
http://www.parashift.com/c++-faq-lit...tml#faq-10.10), but you
can *use* them there. Regarding the "other data members," see this FAQ:
http://www.parashift.com/c++-faq-lit....html#faq-10.7
> Of
> course,one can always move the initialization away from the
> constructor ,
Right, to enforce proper initialization, especially in the case that
the constructor needs to call a virtual function. This is generally
superior to forcing the user to call an Init() function, which can
easily be forgotten, leaving the object uninitialized. This example is
drawn from Sutter and Alexandrescu's _C++ Coding Standards_ (Item 49):
class B // Hierarchy root
{
protected:
B() { /*...*/ }
// Called right after construction
virtual void PostInitialize() { /*...*/ }
public:
// Interface for creating objects
template<class T>
static std::auto_ptr<T> Create()
{
std::auto_ptr<T> p( new T );
p->PostInitialize();
return p;
}
};
// A derived class
class D : public B { /*...*/ };
// Creating an initialized D object
std::auto_ptr<D> p = D::Create<D>();
> but that is not the goal:
>
> UP_SQLPrepQuery::StatementInternals::StatementInte rnals(bool & status)
> : nParams(0),
> capacity(0),
> tuple_num(0),
> alter_session_statement(false),
> select_statement(false)
> {sprintf(stmt_internals->stmt_name,"%llx",embeddedConnection.GetConnection Internals(__LINE__,
> __FILE__)->prep_cnt++);
> }
>
> The error is in the line:
>
> sprintf(stmt_internals->stmt_name,"%llx",embeddedConnection.GetConnection Internals(__LINE__,
> __FILE__)->prep_cnt++);
> }
>
> Here , I try to get some value through a function.
You have not followed the FAQ on how to post code that doesn't work
(
http://www.parashift.com/c++-faq-lit...html#faq-5.8-). Not
only is it helpful, in this case it is necessary because you don't
provide enough information to divine what you are trying to do. Please
post a *minimal* but *complete* program (i.e., one that we can cut and
paste directly to our editors unchanged) that demonstrates your
problem.
Cheers! --M